Lesson 5

Traffic light

Now it is time to add some new cool features to our race track.

In this lesson we will program Arduino to operate a traffic light.

Below is the final result you will achieve at the end of this lesson:

Traffic light

A traffic light has three color-modes: Green, yellow, and red.

Like the servo motor, the leds of the traffic light are connected to Arduino pins. We need to know which pin is connected to which LED (light):

-Greed LED is connected to pin 15
-Yellow LED is connected to pin 16
-Red LED iד connected to pin 17

Step 1: Turn on green light

Before implementing the traffic light logic, we need to check that the LEDs work and that our code can control them. Let’s start by turning on only the green LED light.

Which block is turning on LEDs?
There is not a specific block in mBlock that turns on LEDs. But we do have a block that can set a pin to HIGH or LOW state.

If we program Arduino to set a pin to a HIGH state, electric current will flow from that pin to the component connected to it.

The component we are connecting is the LED light. If we set the pin to HIGH, electricity will flow into the LED, and the light will turn on.
The LOW state will not send any electric current to the pin, so the light will not turn on.

Add the “Set digital pin as” block to your code and change the pin setting to 15 (green led), as shown in the video below. Then, upload the code to Arduino.

Now that this block is at the end of our code, the green light turns on after the gate opens:

 

Step 2: Turn on yellow & red

We can now turn on the green LED light. But we still need to make sure the yellow and red LEDs work as well.

To turn on the other two lights, add two more “set digital pin” blocks.

You can the blocks by dragging and dropping from the block menu again, or you can duplicate them as shown in this video:

This is the result you should see after uploading the code:

Step 3: Lights off

Now we know how to turn on LEDs, but how do we turn them off?

It’s pretty simple. All we need to do is to set the block parameter to LOW instead of HIGH. Then, Arduino will stop the electric current from flowing through the pin, and the LED will turn off.

Try it yourself!

Our code is currently turning all the lights on.

Let’s wait 3 seconds after the lights turn on and then turn them off.

On your own, try making the changes you need to achieve this.

If you need help, the video below shows the solution:

This should be the result:

Step 4: Traffic light logic

Now we know how to turn lights on and off. But our traffic light is still not behaving as an actual traffic light would.

Watch the following YouTube video, which demonstrates how traffic light switches from red to green. (Pay attention to how the red light is still on when the yellow light turns on!)

Let’s plan our program according to the sequence shown in the video:

1) Arduino turns on.
2) Red light turns on, and the gate closes.
3) Wait 3 seconds (You can change this if you’d like. This is to give you time to place the cars on the ramp before the gate opens).
4) Yellow light turns on (Red should still be on, as we saw in the video).
5) Wait 1 second.
6) Gate opens, the green light turns on, and the red and yellow lights turn off.

 

Make the following changes to the code. Notice how we delete blocks by dragging them to the blocks menu:

Once the code is uploaded, this is the result you should see:

That’s it! Our traffic light is finally working!

In the next lesson we will add sound to our race.