At first, let’s describe in our own native language how a traffic light works and develop its operation algorithm. When we say that a traffic light works, we understand wat we mean by it. But for the controller to run a program, it needs to be described with utmost precision, together with all its operations. For a start, we can give a real-life example of how a traffic light works. How do we cross the road? We have all known it since we were kids. We were taught to look in both directions, etc. However, how do we describe it with
utmost precision? For starters, we approach the crossing and then look left. We should check if there are any cars approaching from the left and if there are none, we cross the road till its center. After that, we should look right and check if there are any cars approaching from the right. If there are none, we can cross the second section of the road. If there are cars approaching, we should wait until they have passed. And only after that should we complete the crossing of the road. In fact, we could have described these actions even in greater detail all the way to how we should move our feet, turn our head, etc. Similarly, we can describe how a traffic light works. So how does it work exactly? Signals of different colors light up alternately. First, the red signal is on, then for a short period of time, nothing happens with the traffic light. After that, together with the red signal, the yellow is actuated. They are now both actuated at the same time for one second. After one second has passed, both signals are off, and the green one is on. Some time passes, during which the green signal is on, then it is put out and actuated again, and it is sort of blinking. After the green signal has finished blinking, the yellow signal is actuated for one second, then it is put out and the red signal is on again. The cycle is afterwards repeated. You already know that actuating a signal involves applying the voltage on a certain contact. I hope that you have already assembled your traffic light model and connected it to Arduino. Now, in fact, you have everything to code the traffic light so that it could work at its full capacity. You can pause the video now, you can look again at the described algorithm and at the example which we analyzed in Linked and write your code on your own. Just don’t forget that apart from the operation stage of the traffic light, there is also a preparatory phase when we have to configure the port which has output LEDs plugged in it. Look here, we have sort of a scheme, which we have translated. At first, we develop an algorithm in our native language, and then we rewrite it in the programming language. The transpiler will then create a computer code which the controller understands. The controller, in its turn, will convert the commands in signals on certain ports, and we shall see how the device works with our own eyes when these signals reach the LEDs of the actuator. Now we can download the final code and see how it works. Let’s look at the code that I have. I hope that you have almost the same one. At first, I determined 3 contacts as outputs. They were the 3 contacts with the in-plugged LEDs. Then in the loop, I started to turn the voltage on and off on these contacts. I started with the pin, which has a red LED connected to it. Then I created a delay while the red LED was on. After that, I actuated the yellow LED together with the red one. They were both on for one second. Then I cut the voltage on both contacts. Let me bring to your notice the fact that the controller doesn’t possess any artificial intelligence, so it won’t do anything until you clearly articulate to it that an action has to be performed. This is especially evident in this fragment. We actuated the red LED and then the yellow one together with it. The red one stayed on, because there was no command about putting it out. The command was given one second later, after both signals were on simultaneously. Also, don’t forget to pay attention to the fact that I am oversimplifying when
I am saying that we have actuated the red or the yellow LED. In reality, the controller does not have the faintest idea about what is connected to its pins. It can only turn the voltage on and off on a certain contact. Let’s proceed. After I put out the red and the yellow signals, I actuate the green one. I wait for three seconds and then put it out too. After that, I repeat the same actions three times, but with a short delay. This is the blinking phase of the green light. I only need to actuate the yellow signal now, and that’s what I’m doing. I then turn it off. The cycle, thus, finishes and recommences again when the red LED is actuated. At this point, we can finally download the code for our first device. However, we need to ensure beforehand that we have chosen the right board and the right port. After that, we start the download. I can’t see any errors at this point. Here we go: red, red, yellow, green, blinking green, yellow and red. And no manual control at all! Congratulations, we have created our first device!