I was trying to develop a traffic light signal controller . when the button is pressed, the pedestrian green light will turn on faster than before . I used delay() in Interrupt function, But it is not working as expected . I am totally new in Arduino coding. My whole code is below:
int carred = 11;
int caryellow = 10;
int cargreen = 9;
int padred = 13;
int padgreen = 12;
int padpushbutton = 3;
int buspushbutton = 2;
void setup(){
pinMode(carred , OUTPUT);
pinMode(cargreen , OUTPUT);
pinMode(caryellow , OUTPUT);
pinMode(padgreen , OUTPUT);
pinMode(padred , OUTPUT);
pinMode(padpushbutton, INPUT_PULLUP);
pinMode(buspushbutton,INPUT);
attachInterrupt(digitalPinToInterrupt(padpushbutton), padcontrol,CHANGE);
attachInterrupt(digitalPinToInterrupt(buspushbutton), buscontrol,CHANGE);
}
void loop()
{
carchangeLights();
}
void carchangeLights(){
//turn carred,padgreen off padred high and cargreen on for 10 seconds
digitalWrite(carred, LOW);
digitalWrite(caryellow,LOW);
digitalWrite(padred, HIGH);
digitalWrite(cargreen, HIGH);
delay(10000);
//cargreen off, turn on caryellow for 4 seconds
digitalWrite(cargreen, LOW);
digitalWrite(caryellow, HIGH);
delay(4000);
// turn caryellow and padred off,turn on padgreen, turn on carred for 7 seconds
digitalWrite(caryellow, LOW);
digitalWrite(padred,LOW);
digitalWrite(padgreen,HIGH);
digitalWrite(carred, HIGH);
delay(7000);
//turn padgreen off and padred high , caryellow high for 3 second
digitalWrite(padgreen, LOW);
digitalWrite(padred, HIGH);
digitalWrite(caryellow, HIGH);
delay(3000);
}
void padcontrol(){
int red = digitalRead(carred);
int yellow = digitalRead(caryellow);
int green = digitalRead(cargreen);
if (red == HIGH && yellow == LOW ) {
digitalWrite(carred, HIGH);
delay(2000);
digitalWrite(padred, LOW);
digitalWrite(padgreen, HIGH);
delay(15000);
}
else if(yellow == HIGH && red == LOW){
digitalWrite(caryellow, HIGH);
delay(2000);
digitalWrite(caryellow,LOW);
digitalWrite(carred,HIGH);
digitalWrite(padred,LOW);
digitalWrite(padgreen,HIGH);
delay(15000);
}
else if(yellow == HIGH && red == HIGH) {
digitalWrite(caryellow, HIGH);
delay(1000);
digitalWrite(caryellow,LOW);
digitalWrite(padred,LOW);
digitalWrite(padgreen,HIGH);
delay(15000);
}
else{
digitalWrite(cargreen,LOW);
digitalWrite(caryellow,HIGH);
delay(3000);
digitalWrite(caryellow,LOW);
digitalWrite(carred,HIGH);
digitalWrite(padred,LOW);
digitalWrite(padgreen,HIGH);
delay(15000);
}
}
void buscontrol(){
int red = digitalRead(carred);
int yellow = digitalRead(caryellow);
int green = digitalRead(cargreen);
if (red == HIGH && yellow == LOW ) {
digitalWrite(carred, HIGH);
delay(3000);
digitalWrite(carred,LOW);
digitalWrite(padgreen, LOW);
digitalWrite(padred, HIGH);
digitalWrite(caryellow, HIGH);
delay(2000);
digitalWrite(caryellow,LOW);
digitalWrite(cargreen, HIGH);
delay(10000);
}
else if(yellow == HIGH && red == LOW) {
digitalWrite(padgreen,LOW);
digitalWrite(padred,HIGH);
digitalWrite(caryellow, HIGH);
delay(2000);
digitalWrite(caryellow,LOW);
digitalWrite(cargreen,HIGH);
delay(10000);
}
else if(yellow == HIGH && red == HIGH) {
digitalWrite(caryellow, HIGH);
delay(1000);
digitalWrite(carred,LOW);
digitalWrite(caryellow,LOW);
digitalWrite(padgreen,LOW);
digitalWrite(padred,LOW);
digitalWrite(cargreen, HIGH);
delay(10000);
}
else{
digitalWrite(padred,HIGH);
digitalWrite(cargreen,LOW);
delay(10000);
}
}