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;
}