Phuong Vo
Published © MIT

Traffic Lights and Push Button

Start by doing a simple traffic light, then add a button to control when it turns red. This is a good project for people starting Arduino.

BeginnerFull instructions provided1 hour67,567
Traffic Lights and Push Button

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Resistor 330 ohm
Resistor 330 ohm
×4
LED (generic)
LED (generic)
Best if it is in different colors but 3 reds are fine
×3
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Jumper wires (generic)
Jumper wires (generic)
This may differ depending on your method
×12

Story

Read more

Schematics

Schematic - Traffic Lights without Button

Traffic Lights with Button

The circuit is the same for the 'improvement to delay' section.

Code

Traffic Lights without Button

Arduino
int red = 3;
int yellow = 4;
int green = 5;


void setup() {
  
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);
}


void loop() {
 changeLights();
 delay(12000);

}

void changeLights(){
    // green off, yellow on for 3 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(red, LOW);
    delay(3000);

    // turn off yellow, then turn red on for 5 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);
    delay(4000);

    // red and yellow on for 2 seconds (red is already on though)
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(red, HIGH);
    delay(2000);

    // turn off red and yellow, then turn on green
    digitalWrite(green, HIGH);
    digitalWrite(yellow, LOW);
    digitalWrite(red, LOW);
    delay(3000);
}

// I got this code from a website called makeusof. I'd recommend checking it out.

Traffic Lights with Button

Arduino
int button = 7;
int red = 3;
int yellow = 4;
int green = 5;


void setup() {
  
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(button, INPUT);
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);
}


void loop() {
 if (digitalRead(button) == HIGH){
        delay(15); // software debounce
        if (digitalRead(button) == HIGH) {
            // if the switch is HIGH, ie. pushed down - change the lights!
            changeLights();
            delay(12000);
        }
    }
 }

void changeLights(){
    // green off, yellow on for 2 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(red, LOW);
    delay(3000);
    
    // turn off yellow, then turn red on for 6 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);
    delay(4000);

    // red and yellow on for 2 seconds (red is already on though)
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(red, HIGH);
    delay(2000);

    // turn off red and yellow, then turn on green
    digitalWrite(green, HIGH);
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);
    delay(3000);
}

Traffic Lights with Button - improvement to delay

Arduino
int button = 7;
int red = 3;
int yellow = 4;
int green = 5;


void setup() {
  
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(button, INPUT);
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);
}


void loop() {
 if (digitalRead(button) == HIGH){
        delay(15); // software debounce
        if (digitalRead(button) == HIGH) {
            // if the switch is HIGH, ie. pushed down - change the lights!
            changeLights();
        }
    }
    else {
      digitalWrite(green, HIGH);
      digitalWrite(red, LOW);
      digitalWrite(yellow, LOW);
    }
 }

void changeLights(){
    // green off, yellow on for 2 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(red, LOW);
    // this is to check if button is still pressed while delaying 
    for(long int i = 0; i < 200000; i++){
      // if button is released
      if (digitalRead(button) == LOW) {
        return;
      }
    }
    
    // turn off yellow, then turn red on for 6 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);
    for(long int i = 0; i < 600000; i++){
      // if button is released
      if (digitalRead(button) == LOW) {
        return;
      }
    }

    // red and yellow on for 2 seconds (red is already on though)
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);
    digitalWrite(red, HIGH);
    for(long int i = 0; i < 200000; i++){
      // if button is released
      if (digitalRead(button) == LOW) {
        return;
      }
    }

    // turn off red and yellow, then turn on green
    digitalWrite(yellow, LOW);
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
    for(long int i = 0; i < 600000; i++){
      // if button is released
      if (digitalRead(button) == LOW) {
        return;
      }
    }
}

Credits

Phuong Vo

Phuong Vo

4 projects • 10 followers
Hi

Comments