/ arduino

Arduino Traffic Light

This traffic light is based on the first example. The same resistor + LED is replicated three times and attached to the different pins.

The very common logic for traffic light is "STOP" for some time, then "CAUTION" for shorter time, and then "GO" for same time as "STOP". There are some regional differences though, so please feel free to adapt it to your location an send me the code.

#define LED_RED 13
#define LED_YELLOW 12
#define LED_GREEN 11
#define TIME_GO 4000
#define TIME_CAUTION 1000
#define TIME_STOP 4000

void setup() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
}

void loop() {
digitalWrite(LED_RED, HIGH);
delay(TIME_STOP);
digitalWrite(LED_RED, LOW);

// yellow / caution
digitalWrite(LED_YELLOW, HIGH);
delay(TIME_CAUTION);
digitalWrite(LED_YELLOW, LOW);

// you can go / green
digitalWrite(LED_GREEN, HIGH);
delay(TIME_GO);
digitalWrite(LED_GREEN, LOW);

// and now back you yellow
digitalWrite(LED_YELLOW, HIGH);
delay(TIME_CAUTION);
digitalWrite(LED_YELLOW, LOW);

// and back to red on the next iteration
}


Alex Netkachov

Alex likes functional programming, algorithms and code reviews. Apart from programming, his favourites are walking with his family in the parks and national trails and reading books.