I am combining three simple projects into one and am having a little difficulty. The first is two traffic lights simulating an intersection which cycle accordingly. The second is a pedestrian crosswalk which illuminates the red crossing light until a button is pressed. The third is a relay which is on while the pedestrian light is red. I have the traffic lights cycling properly. Disregard the delay times because I still need to adjust them after I get it to function properly. The pedestrian red light is illuminated as the lights cycle and the relay is on which is correct. The problem is that I get no response from the push button unless i hold it for an extended period of time (10-20 seconds). It has to be something simple that I am just overlooking. I am new to arduino and if anyone can give me some feedback or point me in the right direction it would be greatly appreciated.
Thanks
void changeLights();
//TRAFFIC LIGHTS
int R1RED = 5;
int R1YEL = 4;
int R1GRE = 3;
int R2RED = 2;
int R2YEL = 1;
int R2GRE = 0;
int num;
//RELAY
int relayPin = 9;
//BUTTON & CROSSWALK
int button = 10; //button pin
int buttonState;
int pedRed = 8; //assign the pedestrian lights
int pedGreen = 7;
void setup() {
pinMode(R1RED, OUTPUT);
pinMode(R1YEL, OUTPUT);
pinMode(R1GRE, OUTPUT);
pinMode(R2RED, OUTPUT);
pinMode(R2YEL, OUTPUT);
pinMode(R2GRE, OUTPUT);
//RELAY
pinMode(button, INPUT);
pinMode(relayPin, OUTPUT);
//CROSSWALK
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
}
void loop() {
// read the state of the switch into a local variable:
buttonState = digitalRead(button);
// only toggle the if the new button state is HIGH
if (buttonState == HIGH) {
changeLights();
}
else(num == 0);{
if(num == 0){
digitalWrite(2, LOW);//R2RED off
digitalWrite(5,HIGH);//R1RED on
digitalWrite(0,HIGH);//R2GRE on
digitalWrite(8,HIGH);//pedRed on
delay(4000);
num = 1;}
if(num == 1){
digitalWrite(0,LOW);//R2GRE off
digitalWrite(1, HIGH);//R2YEL on
delay(3000);
num = 2;}
if(num == 2){
digitalWrite(1,LOW);//R2YEL off
digitalWrite(2,HIGH);//R2RED on
delay(1000);
num = 3;}
if(num == 3){
digitalWrite(5,LOW);//R1RED off
digitalWrite(3,HIGH);//R1GRE on
delay(4000);
num = 4;}
if(num == 4){
digitalWrite(4,HIGH);//R1YEL on
digitalWrite(3,LOW);//R1GRE off
delay(3000);
num = 5;}
if(num == 5){
digitalWrite(4,LOW);//R1YEL off
digitalWrite(5,HIGH);//R1RED on
delay(1000);
num = 0;}
}
}
void changeLights() {
digitalWrite(5, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
digitalWrite(0, LOW);
digitalWrite(4, HIGH);
digitalWrite(1, HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
delay(3000); //wait 3 seconds
digitalWrite(4, LOW);
digitalWrite(1, LOW);
digitalWrite(5, HIGH);
digitalWrite(2, HIGH);
delay(1000); //wait 1 second till its safe
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
delay(5000); //wait for 5 seconds
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
delay(1000);//wait 1 second
}