agsdix-fas fa-child

Flashing traffic light

With this activity, you will learn to automate a traffic light, making it flash yellow before turning red.

info outline icon
Basic Activity; Loop Repetition; Main Loop; Timing.

What are we going to do?

Are you a teacher?

Lesson Plan

Courses

  • Grades 3-12
  • Courses 3rd grade – 12th grade

Materials

  • Cellphone, tablet, or computer
  • Internet connection

Description

In this activity, students can experiment with Protobject, learning about visual programming and computer science safely and playfully using a device.

Educational Objectives

  • Understand the concept of “loop,” both general and nested.
  • Develop a technological object (prototype) using a device.
  • Identify relationships between technology and the surrounding environment.
  • Evaluate one’s own work and that of others, both individually and in teams.
  • Participate in dialogues and reflections to propose improvements.

Introduction (10 minutes)

Welcome students to the class and briefly introduce the day’s activity:

“Today, we will learn how to time an LED lamp.”

We’ll start the class by providing technical knowledge to students for meaningful understanding of the practical activity.

Do you remember how we created a traffic light in the previous activity?

“To make the traffic light work, we assigned on and off times. But what if we wanted the yellow light to flash before turning green? We could simply add more blocks to turn the yellow light on and off several times, but that would make the code very long!”

“To solve this problem, we use Loops. In the previous activity, we saw the main Loop, and this time, we’ll explore the concept of a more general Loop. It’s important to understand that the main Loop is just one type of Loop. Specifically, we’ll address the topic of a numbered Loop, which repeats a block of code a fixed number of times.”

Then, engage the students:

“Are you ready to learn how to use Loops to make the traffic light flash?”

Development (20-30 minutes)

Connect the equipment to the Protobject platform so that students can interact with the LED on the screen using programming commands.

Guide the students through the steps of the “Prototyping” section. Ensure you’ve experimented beforehand to anticipate student questions.

 

Conclusion (5-10 minutes)

Now that you’ve learned to use milliseconds,

Where can we see Loops in everyday life?
What other prototype can you think of creating with this new information?
Can you think of another way to use Loops?
How can we stop a Loop?

That’s what we’ll tackle next!

The challenge is to add a second synchronized traffic light to simulate an intersection. The process is similar to the one we did earlier, with the addition of another Loop to control the flashing of the yellow light.

Check out the suggested solution..

agsdix-fas fa-book-reader

Learn

How can we program a flashing light?

Do you remember how we created a traffic light in the previous activity?

When we time the traffic light, we specify how long each color should stay on before switching to the next one, repeating this entire process in the main loop.

However, what if we wanted to make the yellow light flash before turning red?

This way, we can alert the driver more effectively to stop the car more safely.

So, do we add more blocks to the main loop?

Following this idea, we need to change something in the Main Loop:

Green → Yellow → Turn Off → Yellow → Turn Off → Yellow → Turn Off → Red

It would be sufficient to repeat the sequence of blocks “Yellow → Turn Off” to turn on and off the yellow light as many times as needed… but once again, the code would become very long!

The computer gets confused too!

Previously, we fixed this issue using the Main Loop.

However, let’s remember that the Main Loop is the repetition of the program itself indefinitely… We can’t have more than one!

If we did that, the computer wouldn’t know which one is the main program, and it wouldn’t know what to run.

Let’s repeat some things!

The general concept of a loop is useful here!

It allows us to repeat the code within it a certain number of times (in the image, 3 times).

The main loop is a special version of the loop!

Furthermore, we can nest loops. This means putting one loop inside another… Repetitions within repetitions!

Flashing Yellow: Repetition Loop Inside the Main Loop

Using a loop, we can repeat the blocks that control the sequence “Yellow → Turn Off” as many times as needed.

And by putting everything inside the main loop, the final result would be:

Main Loop (Green → Loop (Yellow → Turn Off [x4])  → Red)

Let’s get to work!

Within our main loop, we will use a secondary loop to repeat a specific part of the code before continuing with the entire code. This will allow us to change the traffic light from green to yellow and create a blinking effect on the yellow light using an internal loop. Later, the main loop will regain control when the light turns red, and this process will repeat infinitely.

To achieve the blinking effect on the yellow light, we will set a short on-off interval. This is achieved with a delay of only 400 milliseconds. For the other light changes, we will assign longer delays, in the range of 4 to 6 seconds.

Make

Let’s create our traffic light.

1

Press Add Device and select Lamp.

2

Scan the QR code

3

Remember that if you don’t have a smartphone to scan the QR codes, you can press Open in this window to open the components on the same computer.

We are ready to start prototyping!

Code composition

Click on the question circle icon to open the comments explaining the code.
agsdi-bulb-options

Reflect

Now that you’ve created a flashing traffic light, ask yourself:

Where can we see loops in everyday life?
Can you think of another way to use loops?

plus square icon

Challenge: Let’s make 2 traffic lights at an intersection work in sync!

When one is red, the other should be green and vice versa.

Let’s continue discussing loops in the next activity!