/ 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.

Screen-Shot-2017-09-02-at-23.59.30

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() {
  // start with red / stop
  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 Netkachov

Alex Netkachov is a Senior Software Developer, currently working in Central London on new generation of energy trading solutions for brokers, traders and exchanges.

Read More

Why not to chat about this post? Join Telegram group Alex@Net or message on Twitter to alex_at_net. Alternatively, use the comments form below.