Home    Worksheets    Subroutines    Picaxe Maths
 

Help sheet 113:
Variable states

This sheet shows you how to use variables to control steering, length etc.

Suppose you want the size of a pattern to increase or decrease each time it's drawn.

One way of doing this is to write lots of versions of the program to draw the pattern, each with larger and larger numbers after the pauses, to gradually increase the size
 
So if you had to produce this:
(a succession of larger squares)

You'd have to write about 7 versions of the program. (count the squares)


1)  7 Gosubs:
 
main:
'------- start of program
          gosub fwd
          gosub fwd
          gosub fwd
          gosub fwd
          gosub fwd
          gosub fwd
          gosub fwd
'----------end of main program---
                   
 
This program calls the fwd subroutine seven times in a simple straightforward way.

It takes longer to write.

It is not so easy to check whether you've done the right number.

It uses "gosub fwd" to run forwards. 
It calls it 7 times to go the required distance. 


Another is to use the built-in loop in Picaxe Basic called a "for" loop.

2)  for b0=1 to 7 and 1 Gosub:
 
main:
'------- start of program
          for b0=1 to 7
          gosub fwd
          next b0
'----------end of main program---

                   

 
The for loop starts by setting b0 to 1.

It increases b0 automatically each time round the loop

It exits the loop when b0 has reached the limit value

the for command  "gosub fwd" sets b0 at the start of the loop
The
"next b0" increases b0 by 1.
The
"next b0" then checks whether b0 has reached the upper limit. If not it loops. 


Content Scl Version 19/04/07