So I'm trying to make a traffic light intersection with two green LEDs, two yellow LEDs, and two red LEDs. I also have a piezo speaker (buzzer) and a push button. I want the traffic lights to run normally and if the push button is pressed, I want the piezo to buzz, the lights to turn red, the lights to STAY red for 3 seconds, and to return to loop after all of that. I'm also using the Arduino TimerOne library.
Now the problem is that I can't get the lights to stay red for 3 seconds. My question is "how can I get my lights to stay red for 3 seconds?".
Here's my code:
#include "TimerOne.h" /* includes TimerOne library */
int red1 = 4; /* Red LED (for North-South road) connected to Digital pin 4 */
int yellow1 = 3; /* Yellow LED (for North-South road) connected to Digital pin 3 */
int green1 = 9; /* Green LED (for North-South road) connected to Digital pin 9 */
int red2 = 5; /* Red LED (for East-West road) connected to Digital pin 5 */
int yellow2 = 6; /* Yellow LED (for East-West road) connected to Digital pin 6 */
int green2 = 7; /* Green LED (for East-West road) connected to Digital pin 7 */
int g1r2delayTime = 5000; /* stores Delay for when North-South lights are GREEN & East-West lights are RED */
int y1r2delayTime = 3000; /* stores Delay for when North-South lights are YELLOW & East-West lights are Red */
int r1r2delayTime = 1000; /* stores Delay for when North-South lights are RED & East-West lights are RED */
int r1g2delayTime = 5000; /* stores Delay for when North-South lights are RED & East-West lights are GREEN */
int r1y2delayTime = 1000; /* stores Delay for when North-South lights are RED & East-West lights are YELLOW */
int alarm = 8; /* Piezo Speaker (Buzzer) connected to Digital pin 8 */
const int switch1 = 2; /* Push Button (switch1) connected to Digital pin 2 */
int switch1State = 0; /* stores Push Button (switch1) reading */
long debounceDelay = 200; /* the Debounce time is 200 milliseconds */
long lastDebounceTime = 0;
int i = 0;
int BlinkState = 0;
// digitalWrite(5, digitalRead(5) ^ 1);
void buttonPress() {
switch1State = digitalRead(switch1); /* store the state of the Push Button in "switch1State" */
}
void setup() {
Timer1.initialize(10); /* initializes timer1, and set a 10 millisecond period */
Timer1.attachInterrupt(alarmSystem); /* attaches Blink() as a timer interrupt function */
Serial.begin(9600); /* opens the Serial port at 9600 b/s */
pinMode(red1, OUTPUT); /* initializes RED LED (for North-South road) as an ouput */
pinMode(yellow1, OUTPUT); /* initializes YELLOW LED (for North-South road) as an output */
pinMode(green1, OUTPUT); /* initializes GREEN LED (for North-South road) as an output */
pinMode(red2, OUTPUT); /* initializes RED LED (for East-West road) as an ouput */
pinMode(yellow2, OUTPUT); /* initializes YELLOW LED (for East-West road) as an output */
pinMode(green2, OUTPUT); /* initializes GREEN LED (for East-West road) as an output */
pinMode(alarm, OUTPUT); /* initializes Piezo Speaker (Buzzer) as an output */
pinMode(switch1, INPUT); /* initializes Push Button as an input */
attachInterrupt(digitalPinToInterrupt(switch1), buttonPress, CHANGE); /* if value of "switch1" changes, run function "buttonPress()" */
}
void BlinkStateCheck() {
if (BlinkState == 1) {
BlinkState = 0;
loop();
}
}
void redlights() {
digitalWrite(red1, HIGH); /* North-South lights are RED & East-West lights are RED */
digitalWrite(yellow1, LOW);
digitalWrite(green1, LOW);
digitalWrite(red2, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(green2, LOW);
}
void alarmSystem() {
// switch1State = digitalRead(switch1);
if (((millis() - lastDebounceTime) > debounceDelay) && (switch1State == HIGH)) {
tone(alarm, 800, 1000);
redlights();
BlinkState = 1;
lastDebounceTime = millis(); /* store the current time in "lastDebounceTime" */
}
}
void loop() {
noTone(alarm);
BlinkStateCheck();
digitalWrite(red1, LOW); /* North-South lights are GREEN & East-West lights are RED */
digitalWrite(yellow1, LOW);
digitalWrite(green1, HIGH);
digitalWrite(red2, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(green2, LOW);
delay(g1r2delayTime);
BlinkStateCheck();
digitalWrite(red1, LOW); /* North-South lights are YELLOW & East-West lights are RED */
digitalWrite(yellow1, HIGH);
digitalWrite(green1, LOW);
digitalWrite(red2, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(green2, LOW);
delay(y1r2delayTime);
BlinkStateCheck();
digitalWrite(red1, HIGH); /* North-South lights are RED & East-West lights are RED */
digitalWrite(yellow1, LOW);
digitalWrite(green1, LOW);
digitalWrite(red2, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(green2, LOW);
delay(r1r2delayTime);
/* Midway(ish) */
BlinkStateCheck();
digitalWrite(red1, HIGH); /* North-South lights are RED & East-West lights are GREEN */
digitalWrite(yellow1, LOW);
digitalWrite(green1, LOW);
digitalWrite(red2, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(green2, HIGH);
delay(r1g2delayTime);
BlinkStateCheck();
digitalWrite(red1, HIGH); /* North-South lights are RED & East-West lights are YELLOW */
digitalWrite(yellow1, LOW);
digitalWrite(green1, LOW);
digitalWrite(red2, LOW);
digitalWrite(yellow2, HIGH);
digitalWrite(green2, LOW);
delay(r1y2delayTime);
BlinkStateCheck();
digitalWrite(red1, HIGH); /* North-South lights are RED & East-West lights are RED */
digitalWrite(yellow1, LOW);
digitalWrite(green1, LOW);
digitalWrite(red2, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(green2, LOW);
delay(r1r2delayTime);
}
I'm a beginner so please go easy on me. Any ideas? Thanks!