Home    Worksheets    Subroutines   

Worksheet 106:
Count-Down Loop

In this worksheet you will use the variable in a different way to count the number of
 times you go round a loop.

1) Start up the PICAXE Programming Editor program:  click here
  - Make sure that PICAXE-18x is selected (in View / Options...)
  - You should see the BASIC page. (if not, Click the Basic button.. )

2) Get a Copy of the subroutines "swonoff" and "fwd":
Click here  and here to go to the subroutines page.
Use the Copy to clipboard button to copy each subroutine, and then paste them into Programming Editor.
(Use the Back button to return to this page.)
 

3) Adjust the "fwd" subroutine
The
fwd subroutine should make the buggy move a small distance forwards, keeping it moving in a straight line. However it needs the steering to be adjusted.
The "
let pins=%XXXX0000" and the "pause x" need to be set to values to suit your buggy.
consult your journal to find the values that worked best from Worksheet 104


3)  Count Down Loop:

This loop does things differently. It still uses variable b0 to count with,
   -  only this time it counts down
( "dec b0" ) instead of up.
It therefore has to set b0 to the number of loops before it starts.

    - note that the loop exits when the
"while" is not true.
 
 

'--- Count Down Loop -----------
main:   
      gosub swonoff
      let b0=30          ' set count to 30
'--------------------------------
      do while b0 > 0
        gosub fwd        ' move forwards
        dec b0           ' count down by 1
      loop
'--------------------------------
      let pins=%00000110 ' LED ON
      stop
'----------end of main program---
 
                         
 
This program sets the loop count before looping

It then loops 30 times, Moving forward slightly each time,
 
">" means " greater than "  - so  it is checking whether the count has reached 0.

The fact that you don't have to change the decision to alter the distance travelled
means that you can have a subroutine which moves forward a distance controlled by a variable.
The name for this type of  variable  is a parameter.
Copy this main program into Programming Editor above the subroutines.

3)  Download to your Buggy

Test that it runs forward for a distance and then stops and lights the LEDs.
It should run for 3 seconds.  You can adjust the distance by the value b0 is set to at the beginning.


4) In future worksheets:
 

We'll be using subroutine "fwdb0" to move the buggy forwards a known distance in a straight line.

Click here to see the flowchart.

 

Here is the main program re-written to use the subroutine:

 
'--------------
main:    gosub swonoff
         let b0=30     ' set count to 30
         gosub fwdb0
         let pins=%00000110   ' LEDs on
         stop
'----------end of main program---

 
                         
 
The main program sets the loop count(b0) before calling fwdb0

fwdb0 then loops b0 times, calling fwd each time,

The main program lights the LEDs at the end.

 


 

Assessment :
Before  you've finished this worksheet, you need to show the teacher your work.

1) Keep this page on the screen with your answers filled in, so that the teacher can mark your work.

2) If you are told to, print out this page..     and hand it in for marking.

 

Content Scl Version 2/3/07