I'm having trouble figuring out what I'm doing wrong with my sketch. I created programs in my sketch that I call with each other (a program runs and at the end of its cycle, it calls the next program). Its two "traffic lights" that interchange condition that can be interrupted with a push button to make both traffic light red (stop traffic) and make one pedestrian light green. My current thinking was an if statement where I check the status of the pushbutton INPUT and if detected, compare it to the current program running. If program A is running, run Program C, if Program B is running run program D for example.
I include my wiring schematic and code. I just need to understand how to do this. I was thinking of another way using a Boolean statement. I will then set a int to say "1". Then if the INPUT pin is found to be HIGH then I change the int to say "2" and then compare the INPUT pin and the int to initiate which ever program. I hope the question makes sense.
Point is I think I am comparing elements in my if statement that I can't compare, where I should rather...this is where I need help
My code
int pinLed=13; //Verify that button works
int carRedn=12; //Traffic North Red
int carGreenn=11; //Traffic South Green
int carReds=10; //Traffic North Red
int carGreens=9; //Traffic South Green
int pedRed=8; //Pedestrian Green
int pedGreen=7; //Pedestrian Green
int button=6; //Pedestrian button
void setup(){
pinMode(pinLed,OUTPUT);
pinMode(carRedn,OUTPUT);
pinMode(carGreenn,OUTPUT);
pinMode(carReds,OUTPUT);
pinMode(carGreens,OUTPUT);
pinMode(pedRed,OUTPUT);
pinMode(pedGreen,OUTPUT);
pinMode(button,INPUT);
northGo();
}
void loop(){
int state=digitalRead(button); //Reads the condition of input
if(state == HIGH&&(northGo)){ //Compares button condition with running program
digitalWrite(pinLed,HIGH); //Button check
delay(250);
digitalWrite(pinLed,LOW);
northpedgo(); //Since northGo is running, calls cancel program northpedgo
}
if(state == HIGH&&(southGo)){ //Compares button condition with running program
digitalWrite(pinLed,HIGH); //Button check
delay(250);
digitalWrite(pinLed,LOW);
southpedgo(); //Since southGo is running, calls cancel program southpedgo
}
if(state == HIGH&&(allRed)){ //Compares button condition with running program
digitalWrite(pinLed,HIGH); //Button check
delay(250);
digitalWrite(pinLed,LOW);
pedGo(); //Since allRed is running, calls cancel program pedGo
}
if(northGo){
northsouth();
}
if(southGo){
southnorth();
}
}
int allRed(){ //Everyone stops
digitalWrite(carRedn,HIGH);
digitalWrite(carGreenn,LOW);
digitalWrite(carReds,HIGH);
digitalWrite(carGreens,LOW);
digitalWrite(pedRed,HIGH);
digitalWrite(pedGreen,LOW);
delay(2000);
}
int northGo(){ //Northen traffic go, South & Pedestrian stop
digitalWrite(carRedn,LOW);
digitalWrite(carGreenn,HIGH);
digitalWrite(carReds,HIGH);
digitalWrite(carGreens,LOW);
digitalWrite(pedRed,HIGH);
digitalWrite(pedGreen,LOW);
delay(10000);
northsouth();
allRed();
}
int southGo(){ //Southern traffic go, North & Pedestrian stop
digitalWrite(carRedn,HIGH);
digitalWrite(carGreenn,LOW);
digitalWrite(carReds,LOW);
digitalWrite(carGreens,HIGH);
digitalWrite(pedRed,HIGH);
digitalWrite(pedGreen,LOW);
delay(10000);
southnorth();
allRed();
}
int pedGo(){ //Traffic stops, Pedestrians go
digitalWrite(carRedn,HIGH);
digitalWrite(carGreenn,LOW);
digitalWrite(carReds,HIGH);
digitalWrite(carGreens,LOW);
digitalWrite(pedRed,LOW);
digitalWrite(pedGreen,HIGH);
pedStop();
allRed();
northGo();
}
int pedStop(){ //Stops pedestrians, returns to NorthGo
digitalWrite(pedRed,HIGH);
delay(250);
digitalWrite(pedRed,LOW);
delay(250);
digitalWrite(pedRed,HIGH);
delay(250);
digitalWrite(pedRed,LOW);
delay(250);
digitalWrite(pedRed,HIGH);
delay(250);
digitalWrite(pedRed,LOW);
delay(250);
digitalWrite(pedRed,HIGH);
delay(50);
allRed();
northGo();
}
int northsouth(){ //Changes traffic from North to South
digitalWrite(carRedn,HIGH);
delay(250);
digitalWrite(carRedn,LOW);
delay(250);
digitalWrite(carRedn,HIGH);
delay(250);
digitalWrite(carRedn,LOW);
delay(250);
digitalWrite(carRedn,HIGH);
delay(250);
digitalWrite(carRedn,LOW);
delay(250);
digitalWrite(carRedn,HIGH);
delay(50);
allRed();
southGo();
}
int southnorth(){ //Changes traffic from South to North
digitalWrite(carReds,HIGH);
delay(250);
digitalWrite(carReds,LOW);
delay(250);
digitalWrite(carReds,HIGH);
delay(250);
digitalWrite(carReds,LOW);
delay(250);
digitalWrite(carReds,HIGH);
delay(250);
digitalWrite(carReds,LOW);
delay(250);
digitalWrite(carReds,HIGH);
delay(50);
allRed();
northGo();
}
int northpedgo(){ //Stops all traffic from NorthGo, pedestrian go
digitalWrite(carRedn,HIGH);
delay(250);
digitalWrite(carRedn,LOW);
delay(250);
digitalWrite(carRedn,HIGH);
delay(250);
digitalWrite(carRedn,LOW);
delay(250);
digitalWrite(carRedn,HIGH);
delay(250);
digitalWrite(carRedn,LOW);
delay(250);
digitalWrite(carRedn,HIGH);
delay(50);
allRed();
pedGo();
}
int southpedgo(){ //Stops all traffic from SouthGo, pedestrian go
digitalWrite(carReds,HIGH);
delay(250);
digitalWrite(carReds,LOW);
delay(250);
digitalWrite(carReds,HIGH);
delay(250);
digitalWrite(carReds,LOW);
delay(250);
digitalWrite(carReds,HIGH);
delay(250);
digitalWrite(carReds,LOW);
delay(250);
digitalWrite(carReds,HIGH);
delay(50);
allRed();
pedGo();
}
