Jewelbots Tutorials Lesson 4: Using the timer function to blink LEDs

At the end of the last lesson , we had turned on LEDs in different colors. That was kind of cool, but what if we wanted to make the LEDs blink?

We can do this by using two functions: turn_off_single and pause.

Theturn_off_single function refers to turning off one LED. This is similar to the turn_on_single function we talked about earlier. Since it’s related to LEDs, it’s also defined as a part of the LED class, so you call it as follows:

led.turn_off_single(POSITION_OF_LED);

Remember, you don’t actually write POSITION_OF_LED— you replace that with the position (NE, SE, NW, SW) of the LED you want to turn off.

Okay! Let’s take the Hello World example, and add a line of code to turn off the LED.

The Hello World example from Lesson 1 was this:

void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
LED led;
led.turn_on_single(NE, BLUE);
}

So now let’s add led.turn_off_single(NE); to turn off the LED that we had turned on. Put it right below led.turn_on_single(NE, BLUE);:

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

What do you expect this code to do? Upload it to your Jewelbot to see if it does what you thought!

When you upload the code and unplug your Jewelbot, you’ll discover that the LED turns on…and stays on. It never seems to turn off, even though we called the turn_off_single function. It’s actually turning on and turning off over and over again so quickly that it looks like it’s constantly on. (You might notice that the light is a little dimmer than it would be if you just had included the line of code to turn on the LED, but not included the line of code to turn off the LED. This is how your eye perceives the light resulting from the LED going on and off really quickly.)

So how can we get the light to blink? We need code to get the light to turn on, and then turn off and stay off for a brief period of time. We do that using the timer functionality on the Jewelbot.

Just as all of the functions for working with LEDs are defined in the LED class, the functions for working with the timer are defined in the Timer class. To use the timer, we have to start out by making an instance of the Timer class:

Timer timer

You can put this in the loop function, right below LED led:

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

The Timer class has just one function we want to use: pause. The function pause causes your Jewelbot to remain in its present state for the amount of time specified. After the amount of time is over, the program you wrote continues to run.

The pause function needs to know how long to pause for. This information — the amount of time to pause for — is put in the parentheses that come after timer.pause.

The unit of time the pause function uses is milliseconds — 1/1000 of a second. So, if you want the Jewelbot to pause for 1 second, which is 1000 milliseconds, you’d put 1000 between the parentheses. Putting information, like the amount of time, in between the parentheses after a function is called passing in that information to the function.

timer.pause(1000) // Pause for 1 second

If you want it to pause for 2 seconds, you’d pass in 2000.

timer.pause(2000) // Pause for 2 seconds

If you want it to pause for half a second, you’d pass in half of 1000, which is 500.

timer.pause(500) // Pause for half a second

Okay! Now let’s add in the pause function after turning off the LED. Let’s pause for 3 seconds, which is 3000 milliseconds.

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

Remember the sketch also includes the void setup() function at the beginning! So now your entire program looks like this:

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

Upload this code to your Jewelbot, unplug your Jewelbot, and you should see the LED turn on briefly, and then turn off for 3 seconds, and then turn on briefly again, and then turn off for 3 seconds, and on and on.

Now, it would be nice if the LED turned on for a few seconds, instead of just flickering briefly. The reason it’s turning on so briefly right now is that by calling the function led.turn_off_single(NE) immediately after the function led.turn_on_single(NE, BLUE) we’re turning off the LED right after turning it on. How can we keep the LED on for a longer period of time? By calling the timer.pause function again, this time between turn_on_single and turn_off_single! Let’s keep the LED on for 2 seconds (2000 milliseconds):

void loop() {
// put your main code here, to run repeatedly:
LED led;
Timer timer;
led.turn_on_single(NE, BLUE);
timer.pause(2000); // This is the line we just added!
led.turn_off_single(NE);
timer.pause(3000);
}

When you upload this code to your Jewelbot you should now see the LED turn on for 2 seconds, and then off for 3 seconds, and then repeat this pattern.

Awesome! You can also try turning on any of the LEDs, in any of the colors, for various amounts of time.

In the next lesson, we’ll talk about two more functions for LEDs: turn_on_all and turn_off_all.