Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino-controlled traffic lights
// from https://pmdway.com/blogs/arduino-projects/fun-with-arduino-controlled-traffic-lights
//
// define the I/O pins that are used on the Arduino
// ... the buttons
#define westButton 5
#define eastButton 12
// the LEDs on the traffic light modules
#define westRed 4
#define westYellow 3
#define westGreen 2
#define eastRed 11
#define eastYellow 10
#define eastGreen 9
#define yellowBlinkTime 500 // 0.5 seconds for yellow light blink
boolean trafficWest = true; // west = true, east = false
int flowTime = 5000; // amount of time to let traffic flow
int changeDelay = 2000; // amount of time between color changes
void setup()
{
// this code runs when the Arduino first powers-up
// or after a reset
// setup digital I/O pins
// buttons are INPUTs
pinMode(westButton, INPUT);
pinMode(eastButton, INPUT);
// LEDs are OUTPUTs
pinMode(westRed, OUTPUT);
pinMode(westYellow, OUTPUT);
pinMode(westGreen, OUTPUT);
pinMode(eastRed, OUTPUT);
pinMode(eastYellow, OUTPUT);
pinMode(eastGreen, OUTPUT);
// set initial state for lights - west side is green first
digitalWrite(westRed, LOW);
digitalWrite(westYellow, LOW);
digitalWrite(westGreen, HIGH);
digitalWrite(eastRed, HIGH);
digitalWrite(eastYellow, LOW);
digitalWrite(eastGreen, LOW);
}
void loop()
// this code runs continuously until power-off or a reset
{
if ( digitalRead(westButton) == HIGH )
// request west to east traffic flow
{
if ( trafficWest != true )
// only continue if traffic flowing in the opposite (east) direction
{
trafficWest = true; // change traffic flow flag to west>east
delay(flowTime); // give time for traffic to clear the bridge
digitalWrite(eastGreen, LOW); // change east-facing lights from green...
// to yellow to red
digitalWrite(eastYellow, HIGH);
delay(changeDelay);
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, HIGH);
delay(changeDelay);
for ( int a = 0; a < 5; a++ ) // blink yellow light
{
digitalWrite(westYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(westYellow, HIGH);
delay(yellowBlinkTime);
}
digitalWrite(westYellow, LOW);
digitalWrite(westRed, LOW); // change west-facing lights from red to green
digitalWrite(westGreen, HIGH);
}
}
if ( digitalRead(eastButton) == HIGH ) // request east to west traffic flow
{
if ( trafficWest == true )
// only continue if traffic flow is in the opposite (west) direction
{
trafficWest = false; // change traffic flow flag to east>west
delay(flowTime); // give time for traffic to clear the bridge
digitalWrite(westGreen, LOW);
// change west lights from green to yellow to red
digitalWrite(westYellow, HIGH);
delay(changeDelay);
digitalWrite(westYellow, LOW);
digitalWrite(westRed, HIGH);
delay(changeDelay);
for ( int a = 0 ; a < 5 ; a++ ) // blink yellow light
{
digitalWrite(eastYellow, LOW);
delay(yellowBlinkTime);
digitalWrite(eastYellow, HIGH);
delay(yellowBlinkTime);
}
digitalWrite(eastYellow, LOW);
digitalWrite(eastRed, LOW); // change east-facing lights from red to green
digitalWrite(eastGreen, HIGH);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.