So I'm making a 2-lane traffic light intersection project, and I need some help with interrupts and how to go ahead with my project.
There are red, yellow, and green lights that will be changing all the time, and I have a button (and when clicked, I want a speaker to buzz, the lights to turn yellow for 1 second, the lights to turn red for 3 seconds, and then the code to continue). I have an interrupt function so that when the button is clicked, my speaker buzzes and my LEDs turn red.
Now my problem is that I can't have a delay in an interrupt, so I can't control how long my yellow lights stay on, and then how long my red lights stay on.
This is my first time using interrupts so I'm not sure what I should do. I'm also relatively new to Arudino, so help is much appreciated. Here's my code:
int red1 = 4; // red1, yellow1, and green1 are for the north-south road LEDs
int yellow1 = 3;
int green1 = 9;
int red2 = 5; // red2, yellow2, and green2 are for the west-east road LEDs
int yellow2 = 6;
int green2 = 7;
int red1state;
int yellow1state;
int green1state;
int red2state;
int yellow2state;
int green2state;
int alarm = 8; // my piezo speaker (buzzer)
int switch1 = 2; // my push button
long lastDebounceTime = 0;
long debounceDelay = 200;
void setup() {
Serial.begin(9600);
pinMode(red1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(alarm, OUTPUT);
pinMode(switch1, INPUT);
digitalWrite(switch1, LOW);
attachInterrupt(digitalPinToInterrupt(switch1), buttonPress, RISING);
}
void buttonPress() // this is my interupt function
{
if ((millis() - lastDebounceTime) > debounceDelay); {
Serial.println("Clicked");
tone(alarm, 800, 1000);
digitalWrite(red1, HIGH);
red1state = HIGH;
digitalWrite(yellow1, LOW);
yellow1state = LOW;
digitalWrite(green1, LOW);
green1state = LOW;
digitalWrite(red2, HIGH);
red2state = HIGH;
digitalWrite(yellow2, LOW);
yellow2state = LOW;
digitalWrite(green2, LOW);
green2state = LOW;
}
lastDebounceTime = millis();
}
void loop() {
digitalWrite(red1, LOW); //green and red
red1state = LOW;
digitalWrite(yellow1, LOW);
yellow1state = LOW;
digitalWrite(green1, HIGH);
green1state = HIGH;
digitalWrite(red2, HIGH);
red2state = HIGH;
digitalWrite(yellow2, LOW);
yellow2state = LOW;
digitalWrite(green2, LOW);
green2state = LOW;
delay(5000);
digitalWrite(red1, LOW); //yellow and red
red1state = LOW;
digitalWrite(yellow1, HIGH);
yellow1state = HIGH;
digitalWrite(green1, LOW);
green1state = LOW;
digitalWrite(red2, HIGH);
red2state = HIGH;
digitalWrite(yellow2, LOW);
yellow2state = LOW;
digitalWrite(green2, LOW);
green2state = LOW;
delay(3000);
digitalWrite(red1, HIGH); //red and red
red1state = HIGH;
digitalWrite(yellow1, LOW);
yellow1state = LOW;
digitalWrite(green1, LOW);
green1state = LOW;
digitalWrite(red2, HIGH);
red2state = HIGH;
digitalWrite(yellow2, LOW);
yellow2state = LOW;
digitalWrite(green2, LOW);
green2state = LOW;
delay(1000);
//Midway(ish)
digitalWrite(red1, HIGH); //red and green
red1state = HIGH;
digitalWrite(yellow1, LOW);
yellow2state = LOW;
digitalWrite(green1, LOW);
green2state = LOW;
digitalWrite(red2, LOW);
red2state = LOW;
digitalWrite(yellow2, LOW);
yellow2state = LOW;
digitalWrite(green2, HIGH);
green2state = HIGH;
delay(5000);
digitalWrite(red1, HIGH); //red and yellow
red1state = HIGH;
digitalWrite(yellow1, LOW);
yellow1state = LOW;
digitalWrite(green1, LOW);
green1state = LOW;
digitalWrite(red2, LOW);
red2state = LOW;
digitalWrite(yellow2, HIGH);
yellow2state = HIGH;
digitalWrite(green2, LOW);
green2state = LOW;
delay(3000);
digitalWrite(red1, HIGH); //red and red
red1state = HIGH;
digitalWrite(yellow1, LOW);
yellow1state = LOW;
digitalWrite(green1, LOW);
green1state = LOW;
digitalWrite(red2, HIGH);
red2state = HIGH;
digitalWrite(yellow2, LOW);
yellow2state = LOW;
digitalWrite(green2, LOW);
green2state = LOW;
delay(1000);
}
Thanks in advance!