Home    Worksheets    Subroutines   
 

Worksheet 105:
Count-Up Loop and Variables

In this worksheet you will learn how to use variables to control the number of
times you go round a loop.

You can use this to make the buggy go forward a known distance.

1) Start up the PICAXE Programming Editor program:  click here
  - Make sure that the following options are selected (in View / Options...)
  -
PICAXE-18x   (Mode tab)
  - Text-mode Colour syntax   (Editor tab)
  - Compiler-Enhanced   (Editor tab)
(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)  Count-Up Loop:
This loop uses Variable b0 to store the count value.  (It doesn't do anything useful inside the loop yet)
Here is  your main program:
 
'---2 Count Up Loop -------------
main:  
        let b0=0            ' set count to 0
'--------------------------------
        do until b0=10     
         inc b0             ' count up by 1
        loop                ' loop back
'--------------------------------
        let pins=%00000110  ' LEDs ON
        stop
'----------end of main program---
                   
 
This program starts by setting the count to zero


It then loops 10 times,

 and finally lights the LEDs

It uses "let b0=0" to start the loop with a count of zero. 
It uses "until b0=10" to control when the loop finishes. 
It uses "inc b0" to step the count up by 1 each time you go round the loop.

This loop does not go on forever. It stops when the count reaches 10.

3) Simulate the loop.

Click on Simulate/Step on the menu bar.

The Simulator is ready to run the program, but won't do anything
until you tell it to step to the next basic command.
Switch on the "Show variables" part of the simulate screen.

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.

 

Step on till you get to the first "do"
What is the value of b0 the first time you get to the "do" command  

Now watch the variable b0 increase each time it passes the line
  "inc b0" in the program

Finally check that it goes to line "
let pins=%00000110" when the count is high enough.
What is the value of b0 when you get to line "let pins=%00000110"  


4) Variables.

In the simulator there is a display of all 14 variables:
these are called b0 to b13.
In the loop above we only use b0.
We could have used any of the other 13.

Some facts:
Variables store numbers, the smallest is 0, the largest is 255.

You can do maths with variables -
  add numbers to variables or subtract them
  add 2 variables together
  multiply and divide variables or numbers.

You can use a variable's value to control a decision
 e.g.    "if b0=99"

All variables are set to zero when the program starts.

 

5) Questions on variables

(You might like to use the let simulator to help you with some of these... )

What do the following "let" commands do:
inc b0     
let b0=b0-1
let b1=b0*2
let b0=b0/2
Variables:
How many variables are there in a Picaxe        
What's the value of b0 when you first switch on?
 

  When You've worked out the answers,
 fill in the "Picaxe Maths" section of your Buggy Journal.


6) Over to you to make it do something useful...

Write a program to call "fwd"  30 times using a count-up loop.
Use subroutine "
swonoff" to wait for the right microswitch before you start.
You'll need to fetch "swonoff" and "fwd" from the subroutines page.
Put these subroutines at the very bottom of your program.

Here is a blank program to get you started:

 
'--- Main program should come first---

'---6 Call fwd 30 times -------------
main:  
         gosub swonoff
         let b0=0     ' set count to 0
'--------------------------------
         do <control of loop>   
'   your program goes in here
'   your program goes in here
'   your program goes in here
         loop
'--------------------------------
loopexit:
         let pins=%00000110  ' LEDs ON
         stop
'--------------------------------------
'--- subroutines should follow here ---
                     
 
This program starts by waiting for the right microswitch

It should exit the loop when b0=30
Inside the loop,
it should call fwd
it should count up using b0

 
After the loop exits, it lights the LEDs

7) Download to your buggy and use it to measure....
How far the buggy goes with 30 calls to fwd.

Run the buggy on a piece of paper.
Line up the back with the end of the paper, and then start the program.
When the buggy stops, mark the new position of the back of the buggy.
Measure the distance travelled with a ruler.
Distance travelled by 30 calls to subroutine "fwd"=mm.

( You can calculate the next 2 if you like - or you can measure the distances by adjusting your program. )
How far does the buggy go with 10 calls to fwd?=mm.
How far does the buggy go with 1 call to fwd?   =mm.

8) Make the buggy move across the baseboard and not bump into the wall
With what you now know, calculate the number of loops needed to cross the baseboard
- from Bay2 to Bay1. (It is 655 mm across)
-
Or if you prefer, sideways aiming towards Bay3 (it's 855mm in that direction)
Modify the program to stop Just before it reaches the other side.
Record this in you journal and in the box below.

Number of "fwd" loops to cross the baseboard Bay2-Bay1  loops

 

Assessment :
You should already be producing a Report as part of the assessment for the
Baseboard Task.

1) Explain how you've used this sheet, and which part of the task it's helped you with.

 

Content Scl Version 23/04/07