Jewelbots Tutorials Lesson 7: Variables

In programming, we are always looking to do things as efficiently as possible with the least repetition of lines of code.

Let’s say you wrote the following program for your Jewelbot:

void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
void button_press() {
LED led;
Timer timer;
led.turn_on_single(NE, BLUE);
timer.pause(2000);
led.turn_off_single(NE);
timer.pause(3000);
led.turn_on_single(NE, BLUE);
timer.pause(2000);
led.turn_off_single(NE);
timer.pause(3000);
led.turn_on_single(NE, BLUE);
timer.pause(2000);
led.turn_off_single(NE);
timer.pause(3000);
}

This will turn on one LED in blue for two seconds, and then turn it off for three seconds, and then and then repeat that sequence two more times.

What if you wanted to modify this a little bit and keep the LED on for one second instead of two, and then keep it off for four seconds instead of three?

As it is right now, we would have to go through and find every time we wrote 2000 and change it to 1000, and then find every time we wrote 3000 and change it to 4000. We could do that, but if the program was long, it would take a while to change each command one by one. We might also forget to update one of the numbers we meant to change, and that wouldn’t be good!

To make things easier, we can store the numbers 2000 and 3000 as variables. Variables are names you give to values in a program. Once you create a variable and set it to a value, the program will know to replace any occurrence of the variable name with the value you set it to.

Here’s how you define a variable.

int ON_TIME = 2000;

The name we chose for this variable is ON_TIME, since it’s the amount of time the LED will stay on. It’s good to come up with names that are descriptive and make sense in the context of your program. We could give this variable any name we wanted — like CAT, for example, but since this program doesn’t have anything to do with cats, that might not be the best name for it.

After ON_TIME we have an equal sign, and then 2000. That’s saying that this variable’s value is 2000 — the amount of time we want the LED to stay on for. After 2000 there’s a semicolon, because that’s how all statements end in the Arduino programming language.

There’s one last part of the statement: the word int at the beginning. That’s something called the variable’s type — the Jewelbot needs to know if the variable is an integer number (like 1, 3, or 12), or a number that has a decimal point (like 5.2 or 11.6), or a letter (like A or B). In this case, the variable’s type is int, or integer — a number with no decimal value. (Numbers with decimals are a variable type called float, and letters are a variable type called char. You probably won’t have a need for the char type when programming your Jewelbot, but chars are useful in other coding contexts.)

We can put this statement int ON_TIME = 2000; above the line void loop() {. We can also define another variable to represent the amount of time the LED will be off — let’s call it OFF_TIME. Since we want the LED to be off for 3 seconds, we wrote int OFF_TIME = 3000.

Okay, let’s put these two lines into our program:

int ON_TIME = 2000;
int OFF_TIME = 3000;
void button_press() {
LED led;
Timer timer;
led.turn_on_single(NE, BLUE);
timer.pause(2000);
led.turn_off_single(NE);
timer.pause(3000);
led.turn_on_single(NE, BLUE);
timer.pause(2000);
led.turn_off_single(NE);
timer.pause(3000);
led.turn_on_single(NE, BLUE);
timer.pause(2000);
led.turn_off_single(NE);
timer.pause(3000);
}

Now, we can go through and replace every time we wrote 2000 with ON_TIME, and every time we wrote 3000 with OFF_TIME:

int ON_TIME = 2000;
int OFF_TIME = 3000;
void button_press() {
LED led;
Timer timer;
led.turn_on_single(NE, BLUE);
timer.pause(ON_TIME);
led.turn_off_single(NE);
timer.pause(OFF_TIME);
led.turn_on_single(NE, BLUE);
timer.pause(ON_TIME);
led.turn_off_single(NE);
timer.pause(OFF_TIME);
led.turn_on_single(NE, BLUE);
timer.pause(ON_TIME);
led.turn_off_single(NE);
timer.pause(OFF_TIME);
}

When your Jewelbot is running this program, every time it sees the ON_TIME it will replace it will 2000, and every time it sees OFF_TIME it will replace it with 3000.

Upload this code to your Jewelbot and try it out!

Now, if you wanted to change the time the Jewelbot is on to one second, and the time it is off to four seconds, you can just change the numbers after the equals signs where you’re defining the variables:

int ON_TIME = 1000;
int OFF_TIME = 4000;

Now the amount of time the LED is on will be one second throughout, and the amount of time it is off will be four seconds throughout. Wasn’t that a lot easier than having to go through the whole program and replace every 2000 with 1000, and every 3000 with 4000?