Project Arduino

Traffic light project on Arduino

I am a beginner in programming and also in the world of Arduino, and I would like to make a code where I have 3 LEDs (red, yellow and green) and two buttons, one to turn on and the other to turn off the circuit. But I’m having problems with shutting down the program, I would like to know what’s wrong with the project and if possible I would like links to articles for me to see similar projects.

int red = 2;
int yellow = 4;
int green = 6;
int buttonpin1 = 10;
int buttonpin2 = 11;

bool ligado = false;
int buttonstate1;
int buttonstate0 = LOW; // lastbuttonstate

unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long debounceDelay = 50;

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

void loop() {
  int reading1 = digitalRead(buttonpin1);
  if (reading1 != buttonstate0) {
    lastDebounceTime1 = millis();
  }

  if ((millis() - lastDebounceTime1) > debounceDelay) {
    if (reading1 != buttonstate1) {
      buttonstate1 = reading1;
      if (buttonstate1 == LOW) {
        ligado = true;
      }
    }
  }

  int reading2 = digitalRead(buttonpin2);
  if (reading2 != buttonstate0) {
    lastDebounceTime2 = millis();
  }

  if ((millis() - lastDebounceTime2) > debounceDelay) {
    if (reading2 != buttonstate1) {
      buttonstate1 = reading2;
      if (buttonstate1 == LOW) {
        ligado = false;
      }
    }
  }

  if (ligado) {
    digitalWrite(red, HIGH);
    delay(2000);
    digitalWrite(red, LOW);

    digitalWrite(yellow, HIGH);
    delay(2000);
    digitalWrite(yellow, LOW);

    digitalWrite(green, HIGH);
    delay(2000);
    digitalWrite(green, LOW);
  } else {
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(green, LOW);
  }

  buttonstate0 = reading2;
}

there have been lots of Arduino Traffic Light project

  • not completely sure about the button logic. don't understand why buttonstate0/1 are used for both buttons, buttonstate0 when setting lastDebouceTime1/2 and buttonstate1 when it expires

  • but because the traffic light code takes 6 secs, there's no need to denounce the buttons because each button sets ligado to a unique state, it doesn't matter how many times it's set to that state. the button logic could be simply

    if (LOW == digitalRead(buttonpin1))
        ligado = true;
    else if (LOW == digitalRead(buttonpin2))
        ligado = false;

Here is clever a simulation of a similar traffic light project.

My main difficulty at the moment is making the button (to turn off) close the circuit effectively. I managed to do this with just one button, but it only closed the circuit when I kept the button pressed for a few seconds and when the loop finished passing through the red LED.

Thank you, I didn’t know about that site and the model you sent me is quite interesting.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.