Home    Worksheets    Subroutines   

Worksheet 103:
Waiting for an event

In this worksheet you will learn how to wait for a  microswitch to be pressed.

This forms the innards of subroutine "swonoff"

1) Start up the PICAXE Programming Editor program:  click here
  - Make sure that PICAXE-18x is selected (in View / Options...)
Go to the Editor tab in options.. 

  - Make sure that Text-mode Colour syntax is selected
  - Make sure that Compiler-Enhanced is selected
(if these aren't set correctly you won't be able to simulate)

  - You should see the BASIC page. (if not, Click the Basic button.. )

2) Enter the following BASIC program in Programming Editor:      
(you don't have to type in the green bits to get it to work.)
  do                                 '
'----------------------------        ^
   do                       '        ^
   loop until input1 = 1    ' loop1  ^
'----------------------------        ^
'----------------------------        ^
   do                       '        ^
   loop until input1 = 0    ' loop2  ^
'----------------------------        ^
  loop                      ' loop 3 ^
This program has 3 loops.

The first and second loops have decisions in them, and can exit.

The third outer loop is infinite

3) Simulate the program in Programming Editor

Click on Simulate/Step on the menu bar.
(If you can't see the Simulate menu, make sure the editor options
are set correctly at Step 1 above

The Simulator is ready to run the program, but won't do anything
until you tell it to step to the next basic command.
There should be a Simulate window on the PC screen like this:

Step round the program.

Use the step button
Each line will be highlighted as it briefly runs.

Practice turning inputs on and off in the simulator:.

Try clicking on the input buttons
Each input will go on and off (change colour)
as you click it.


4) How does the state of input1 change the program flow?                     

Keep clicking on the step button to make the program follow the various loops.  
while you are doing this, try different values of Input1 to see how it affects the flow.

Now fill out the following table:

Loop number

State required of input1 
to be in the loop



What do you have to do with input1 to get to the end of the main loop? 

To stop the simulation click the stop button.

5) Outputs : Get the buggy to do something to test your program.              

 Prepare  Output and Pause commands to make your buggy do something interesting when you press the  microswitch.

Command Function
let pins =%
pause         Wait for 2 seconds **

let pins =%


 **Adjust the pause value to suit the
    movement e.g. spin 180 degrees.
     or go backwards, or forwards
    a certain distance.


   do                                '
'--------------------------          ^
   do                     '          ^
   loop until input1 = 1  ' loop1    ^
'--------------------------          ^
'--------------------------          ^
   do                     '          ^
   loop until input1 = 0  ' loop2    ^
'--------------------------          ^
'    insert your lines
'          here
   loop                   ' loop 3---^




Insert your commands lines after loop2
 and before the last "loop" command of loop 3









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

Press the microswitch and release it to see the action.

7) In future worksheets:
We'll be using subroutine "swonoff" to wait for the microswitch before starting the main part of the program.

Click here to see the flowchart.

We using Subroutines for 3 reasons:
1 - to avoid having to re-type the same bits of program,
2 - to gather together bits of program that do a useful job,
3 - to simplify the main program and make it easier to understand.

In BASIC, we put the subroutines at the end of the program.


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