Variables and Loops
To take advantage of these capabilities, a variable will be needed. A variable, as the name suggests, is a program element that allows an attribute to be varied.
Begin by creating a variable that specifies the number of the LED addressed. Select the Make a Variable option under the Data & Blocks section of the Scripts tab.
Then enter the name “LED” in the New Variable dialog box that appears.
Then create a program with the following steps (as shown in the figure below).
- Set LED to 1
- Repeat 30
- Turn On LED
- Change LED by 1
The Repeat block is found under the Control category of the Scripts tab.
This program initially sets the LED variable to 1. Then it repeats the following steps thirty times:
Turn On LED 1
Increase the LED variable by 1 (making its value 2).
Turn On LED 2
Increase the LED variable by 1 (making its value 3)
A program that repeats an action in this manner is called a loop. If this program were written out line by line, thirty lines of code would be required to turn on all of the LEDs. The use of a variable combined with a loop allows this action to be accomplished with just a couple of lines of code.
Once it has been verified that the code works, it can be saved for re-use at a later date by defining a new block, All_On. The term function is used to refer to a block of code that has been defined in this fashion.