Hello,I am new to programming and arduino. i have began a project that involves creating a double traffic light system that includes two sets of traffic lights, two push buttons for pedestrians and finally two sets of light for pedestrians. I have been working on this for a while now and have came to a dead end. its probably simple but i just cant get it.
I have the stop-go sequence of the lights working but when either of the push buttons are pressed, it must stop the sequence and the car lights must go red then the pedestrian lights can both go green. The trouble i am having is that i cant program it to change when the button is pressed, it just keeps continuing with the sequence.
I have tried many times starting from scratch but it always seems to end up as the same problem.
I have included my code below and sorry for any stupid mistakes and sorry that it is probably written very badly. As i said before i am new to this and any help would be greatly appreciated!
int carRed1 = 13; //car lights being assigned
int carAmber1 = 12;
int carGreen1 = 11;
int carRed2 = 10;
int carAmber2 = 9;
int carGreen2 = 8;
int walkRed1 = 7; //walking lights being assigned
int walkGreen1 = 6;
int walkRed2 = 5;
int walkGreen2 = 4;
int button1 = 2; // button pins
int button2 = 3;
int crossTime = 10000; //crossing time variable given to pedestrians
int flag = digitalRead(button1) or digitalRead(button2);
void setup() {
pinMode(carRed1, OUTPUT); //defining each pin mentioned as either input or output
pinMode(carAmber1, OUTPUT);
pinMode(carGreen1, OUTPUT);
pinMode(walkRed1, OUTPUT);
pinMode(walkGreen1, OUTPUT);
pinMode(button1, INPUT);
pinMode(carRed2, OUTPUT);
pinMode(carAmber2, OUTPUT);
pinMode(carGreen2, OUTPUT);
pinMode(walkRed2, OUTPUT);
pinMode(walkGreen2, OUTPUT);
pinMode(button2, INPUT);
}
void checkCrossing()
{
if(carRed1 == HIGH);{
digitalWrite(carAmber1, LOW);
digitalWrite(carGreen1, LOW);}
if(carRed1 == LOW);{
goRed1();}
if(carRed2 == HIGH);{
digitalWrite(carAmber1, LOW);
digitalWrite(carGreen1, LOW);}
if(carRed2 == LOW);{
goRed2();}
}
void goRed1()
{
delay(1000);
digitalWrite(carGreen1, LOW);
digitalWrite(carAmber1, HIGH);
delay (2000);
digitalWrite(carAmber1, LOW);
digitalWrite(carRed1, HIGH);
}
void goRed2()
{
delay(1000);
digitalWrite(carGreen2, LOW);
digitalWrite(carAmber2, HIGH);
delay (2000);
digitalWrite(carAmber2, LOW);
digitalWrite(carRed2, HIGH);
}
void loop()
{
while (flag == HIGH)
{
checkCrossing();
}
while (flag == LOW)
{
digitalWrite(walkRed1, HIGH);
digitalWrite(walkRed2, LOW);
digitalWrite(carRed1, LOW);
digitalWrite(carRed2, HIGH);
digitalWrite(carAmber1, LOW);
digitalWrite(carAmber2, LOW);
digitalWrite(carGreen1, HIGH);
digitalWrite(carGreen2, LOW);
digitalWrite(walkGreen1,LOW);
digitalWrite(walkGreen2, HIGH);
delay(20000);
digitalWrite(walkGreen2, LOW);
digitalWrite(walkRed2, HIGH);
digitalWrite(carGreen1, LOW);
digitalWrite(carAmber1, HIGH);
delay (2000);
digitalWrite(carAmber1, LOW);
digitalWrite(carRed1, HIGH);
delay(2000);
digitalWrite(carAmber2, HIGH);
delay(2000);
digitalWrite(carRed2, LOW);
digitalWrite(carAmber2, LOW);
digitalWrite(carGreen2, HIGH);
digitalWrite(walkRed1, LOW);
digitalWrite(walkGreen1, HIGH);
delay(20000);
digitalWrite(walkGreen1, LOW);
digitalWrite(walkRed1, HIGH);
digitalWrite(carGreen2, LOW);
digitalWrite(carAmber2, HIGH);
delay (2000);
digitalWrite(carAmber2, LOW);
digitalWrite(carRed2, HIGH);
delay(2000);
digitalWrite(carAmber1, HIGH);
delay(2000);
digitalWrite(carRed1, LOW);
digitalWrite(carAmber1, LOW);
}
}