Home    Worksheets    Subroutines   
 

Worksheet 115:
Try to reach

In this worksheet you will learn how to move certain distance forwards, whilst at the same time checking for an obstacle.

 


1) 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.)


2) Adjust the "fwd" subroutine

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


3)  Count Down Loop that stops on microswitch, or variable value.
This loop is like the one in worksheet 106, but instead of stopping after a set distance,
 - it stops when either microswitch is pressed
or the distance has been reached
 
'-- Count Down with microswitch--
main:   gosub swonoff

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

        do while b0>0 and input1=0 and input0=0
          gosub fwd       'move forward one unit
          dec b0          'count down by 1
        loop 
'----------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, OR b0 reaches zero.

 and finally it turns on the LEDs

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

This program is useful if you want to go a certain distance, but if there's a wall you need to know.
(e.g. in obstacle avoidance)

You can tell the reason it stopped by testing the value in b0.  If it has reached 0 then you didn't hit a wall
If its more than zero, then you've hit a wall and still go b0 units to go.

4) 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. 
Either Press it again to stop the buggy, Or wait for 4 seconds and it'll stop anyway.

(Go back to step 3 if you need to adjust the steering.)

5) Modify the program to switch on the Left LED if it hits something,
and the right LED if it stopped of its own accord
.
copy your program to this box:

6) In future programs:
You can use subroutine "reach" to move forward b0 units, or stop if there's a wall.

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 answer 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 07/06/07