Lesson 17

LED animation

In this lesson, we will make some cool animations with our LEDs. Lets start with something simple – a blinking LED.

Step 1: Blinking LED

 

A blinking LED is just an LED that is turned on for some time and turned off for some time.

To turn off an LED, simply set all the colors to 0.

Lets implement this in our code. Update your code according to the image below:

Because we don’t have a loop in our code (yet), the LED will only blink once.

If you missed seeing the blink after the upload, restart Arduino as shown below:

Step 2: Adding a loop

If we want our LED to blink continuously, we will need to add a loop to our code.

 

We have two loop blocks that we can use: Forever and Repeat.
To limit blinking to a specific amount of times, use the repeat block. Otherwise, you can use the forever loop block.

The code below is using the repeat loop block, but you are welcome to try forever as well.
 

This is the result of our code:

Step 3: Running LED animation

Now that we’ve made blinking LEDs let’s move towards more advanced animations.

We light all the LEDs in the strip with a slight delay in between each LED.

Since we have 15 LEDs, we will need to use the repeat loop and repeat it 15 times. However, we need to find a way to tell the loop to light a different LED each time it runs.

When the loop first runs, we want it to turn on the first LED (address 0). The second loop will turn on the second LED (address 1) and so on until we get to the last LED (address 14).

Essentially, we want the loop to repeat the same code, but we want a part of the code to be a little different each time it runs. To achieve this, we will use variables.

We will create a new variable named “counter” and set its value to 0. Every time the loop runs, the loop will increase the value of our “counter” variable.

Update the code as shown in the video:

  

This is the result you should get:

Now, try playing with the delay time to create a more dramatic animation. We found that a 0.1-second delay works great!

Step 4: LED Show block

When defining the LED’s color using the Set LED block, Arduino will remember the color we specified in its memory. But, it will not update the strip. Only when we add the LED Show block will Arduino update the strip.
To understand why this is a helpful feature, let’s try changing the code a bit.

Move the LED Show block outside of the loop as shown below:

Now, the loop will define each LED color, but the code will only update the LED strip one time after completing the loop.

Upload the code displayed above and see how all the LEDs turn on together after the loop finishes.

Step 5: Better running animation

Now lets improve our running animation. 

The video below shows the desired outcome. Can you update the code to achieve this animation by yourself? Give it a try! (the solution is below the video).

This is the solution code:

Not only we are turning on a different LED every time the loop runs, but we are also turning off the previous LED.

Step 6: Adding the second strip

First, connect the second LED strip as shown in the video.

The second LED strip can connect directly to the first one. Or, you can use the extension cable included in the kit. Position both strips on your race track any way you’d like.

 

Connecting the extension cable :

Adding the second strip to our code is simple.

Since the first strip LEDs have addresses of 0 through 14, the addresses of the second strip’s LEDs will be 15 (first LED) through 29 (last LED).

Then, to light all 30 LEDs, we need to repeat our loop 30 times instead of 15.

 

Change repeat 15  to repeat 30 in your code and the number of LEDs in the Setup LED block:

 

 

This is the result you should get:

 
 

Adding animations to the race game

Now you are familiar with LED animations!

We added some more animations to the final racing game code, which you can download below and upgrade your racing game!

*Please note that LED strips and remote control IR sensors cannot work at the same time. For this reason, the remote control IR sensor was disabled in the code above.

We are done!

Well done! In the next lesson, we will learn about the MP3 player part of the Upgrade Kit and make some noise!