Home    Worksheets    Subroutines   

Worksheet 107:

In this worksheet you will learn how to use variables to count the number of times
you go round a loop, whilst at the same time checking for an

This means you'll be able to make the buggy measure the distance to an obstacle.

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 it into Programming Editor.
(Use the Back button to return to this page.)

) Adjust the "fwd" subroutine
fwd subroutine moves the buggy a small distance forwards, keeping it 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

4)  Count Up Loop that stops on microswitch
This loop is like the one in worksheet 105, but instead of stopping after a set distance,
 - it stops when either microswitch is pressed.
'--11 Count Up with microswitch--
main:   gosub swonoff

'------start of measurement------
        let b0=0         'set count to 0

        do while input1=0 and input0=0
          gosub fwd       'move forward one unit
          inc b0          'count up by 1
'----------end of measurement---
        let pins=%00000110  ' LEDs ON
'----------end of main program---
This program starts by waiting for the right hand microswitch to be pressed and released

It then loops, moving forward until either microswitch is pressed,

 and finally it turns on the LEDs and starts sending the variable values to the PC, using debug.

Use the Copy to clipboard button to copy the main program, and then paste it to the start of your program in Programming Editor.

The new bit is that when it finishes the loop it sends the B0 value to the PC
 - using the debug window.  (You used debug in Worksheet 101 to see the input pins).

5) Download the program to the buggy:
  - connect up the download lead to the socket at the back of the buggy.
  - switch on the buggy,
  - Click on PICAXE/Run or click the button. (or use F5)

The PIC chip is now running the program.
Don't unplug the download lead.

Press the microswitch and release it to start the buggy.  Press it again to stop it.
(Go back to step 3 if you need to adjust the steering.)

6) Use the program to measure short distances near your computer.

After downloading, set the front of the buggy exactly 200mm away from a wall or book.
Start the buggy with the right microswitch in the normal way, and let it stop at the wall.
The PC debug screen should show the number of loops in B0.

Using your result from worksheet 105 (distance travelled by a single call to subroutine
 -  calculate how many mm the buggy thinks it has gone.
Counted number of "fwd" loops   loops.
Distance for each "fwd" loop:    mm (from worksheet 105)
Therefore measured distance:    mm

10) In future programs:
You can use subroutine "measure" to set b0 to the distance a buggy moves
before it hits something.  You can use an "if b0 > " decision to decide what to do next...

Click here to see the flowchart.



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 20/4/07