This was a a simple board that I bought which basically consists of a red, yellow and green LED on a board, rather than buying seperate components and using a breadboard this is a reasonably low cost solution.
Code
In the example we use Pin 5 , 6 and 7 for the green, yellow and red LEDs
[codesyntax lang=”cpp”]
byte pinLedRed = 7; byte pinLedYellow = 6; byte pinLedGreen = 5; void setup() { // Initialize the pins as output pins pinMode(pinLedRed, OUTPUT); pinMode(pinLedYellow, OUTPUT); pinMode(pinLedGreen, OUTPUT); } void loop() { //Red signal digitalWrite(pinLedRed, HIGH); // RED on delay(5000); // 5 secs //Yellow + Red digitalWrite(pinLedYellow, HIGH); // YELLOW on delay(1000); // 1 sec //Green signal digitalWrite(pinLedRed, LOW); // RED off digitalWrite(pinLedYellow, LOW); // YELLOW off digitalWrite(pinLedGreen, HIGH); // GREEN on delay(5000); // 5 secs //Yellow signal digitalWrite(pinLedGreen, LOW); // GREEN off digitalWrite(pinLedYellow, HIGH); // YELLOW on delay(1000); digitalWrite(pinLedYellow, LOW); // YELLOW off }
[/codesyntax]
Link
The traffic light board only costs about $1