To use variable you need to first declare them in the header of your program. The header is the stuff you put before the forever loop.
A simple variable declaration would look like this: var byte x
If you want the variable to have a specific value when the program starts you can do that in the declaration like this: var byte x = 0 or var byte x = 30 Keep in mind that the initial value you assign will only be there the first time the program starts, it won't get that value each time the program starts over after that.
Types of variables include bit, byte, and other larger types we’ll look at later. A bit variable can hold a 1 or a 0. A byte variable can hold an 8 bit binary number, so the byte variable will hold numbers from 0 -255. Here is an example program to modify your blinker program to stay on longer each time the loop restarts. Remember to copy over your header from the previous program. The full code for this program is available here.
pin_b4_direction = output
var byte x = 0 -- makes a variable called x that holds a byte of information and starts at 0
forever loop
pin_b4 = on
delay_1ms(x)
x = x + 5 -- This line adds 5 to x each time the loop repeats.
pin_b4 = off
delay_100ms(2)
end loop -- Each time the led flashes it should stay on longer.
Watch the LED for a minute. Does it continue to get longer and longer forever? Why/Why not? What happens when the x reaches 255 and then the counter tries to add 5 to that?
Try modifying your program to make the LED start by staying on for 3 seconds (3000 ms) and then stay on for a shorter amount of time each time the program loops.
Now make the on time start at 3 seconds and the off time start at 0. The on time should decrease and the off time should increase. (you’ll need to make another variable)
Ready for more? Check out Jal 3 FOR loops and ports