By JeanC117 Follow
We are all familiar with traffic lights -- they are one of our biggest pet peeves, yet creates order in society by giving us all a common understanding needed for navigating cars. Traffic lights are one of the great advances in technologies, many take for granted. Prior to the invention of timed traffic lights, multiple people would have to manually direct the traffic, with a popular example is the London Bridge.
This project introduces the fundamental skill of driving multiple LEDs, understanding timing, and programming digital output signals using an Arduino; learning by design.
Make sure them like traffic lights, where red is on top, followed by yellow, and then green.
// declare the pin wherein the red, yellow and green LEDs are connected, respectively int redPin = 9; int yellowPin = 10; int greenPin = 11;
void setup() < // declare each of the coloured pins as digital output signals pinMode(redPin, OUTPUT); pinMode(yellowPin, OUTPUT); pinMode(greenPin, OUTPUT); >void loop()
// green light
setLights (0, 0, 1); // stay at a green light for 5 seconds delay(5000);// yellow light setLights (0, 1, 0); // stay at a yellow light for 1 second or 1000 milliseconds delay(1000);
// stop light
setLights (1, 0, 0); // stay at a stop light for 5 seconds or 5000 milliseconds delay(5000);// write a custom function that sets a particular coloured LED ON or off void setLights(int red, int yellow, int green) < // sets each of the coloured lights according to the parameters passed by the user // control a colour as they wish by using 1 as high and 0 as low digitalWrite(redPin, red); digitalWrite(yellowPin, yellow); digitalWrite(greenPin, green); >Step 4: Traffic Model Demo
The code is a demonstration of a sped up traffic light, and does not reflect the actual timing of the traffic light, since it would be quite a long and boring video demonstration. Here, it is demonstrated that the light will cycle through green, yellow, then red.
Changing the delays allows for the line of code preceding the delay to continue running until the delay count is finished.
![]()