For this challenge we will create a traffic lights using an Arduino device/board and three LEDs.
We will use C/C++ to create the code to control the Arduino board to reproduce a following traffic lights sequence:
You do not need to have access to an Arduino board. Instead you can use the online Arduino simulator from Tinkercad to recreate the electronic circuit and add the code provided below.
Open Tinkercad in new windowElectronic Circuit
First you will need to recreate the following electronic circuit:
C/C++ Code
Then you will need to use the following code (When using tinkerpad, choose the text option rather than blocks)
// Arduino Trafic Lights - www.101computing.net/arduino-traffic-lights void setup() { pinMode(13, OUTPUT); // Red LED pinMode(12, OUTPUT); // Amber LED pinMode(11, OUTPUT); // Green LED } void loop() { // Green digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, HIGH); delay(3000); // Wait for 3000 millisecond(s) // Amber digitalWrite(12, HIGH); digitalWrite(11, LOW); delay(1000); // Wait for 1000 millisecond(s) // Red digitalWrite(13, HIGH); digitalWrite(12, LOW); delay(3000); // Wait for 3000 millisecond(s) // Red + Blinking Amber digitalWrite(13, HIGH); digitalWrite(11, LOW); for (int i = 0; i < 3; i++) { digitalWrite(12, HIGH); delay(500); // Wait for 500 millisecond(s) digitalWrite(12, LOW); delay(500); // Wait for 500 millisecond(s) } }
Lego Traffic Ligths
If you have access to an Arduino board you can create your own traffic lights and you can plug your LEDs on a breadboard or use lego bricks to create your traffic lights.
You will need:
- 1 arduino board
- 1 breadboard
- 3 resistors (220Ω)
- 3 LEDs (Red, Organe/Yellow, Green)
Make sure that you always connect the cathode of the LED (short leg, flat edge of the LED) to a black wire.