I am a very new Arduino user and have been through a few basic projects.
My latest project is to try to create a basic UK traffic light system.
I have successfully created the circuit and the basic light changing works fine when left to loop automatically - i.e. Red, then Red & Amber, then Green, then Amber, then back to red with simple delays between the appropriate sections.
I then decided to introduce a pushbutton to emulate a pelican crossing which interrupts the Green light delay and goes through Amber, then Red etc. I have used the EZButton function to do this with some success.
This works ok when first pressed, but then when the lights cycles back through the sequence, when it should go back to green it very briefly flashed and then immediately goes back to Amber. To a novice it seems like it thinks the button is still being pressed.
Can anyone shed any light on why this may be? Is there some kind of reset which needs to be done after the button has been pressed?
int red = 9;
int amber = 8;
int green = 7;
ezButton pushbutton(12); // create ezButton object attached to pin 12;
int counter = 1;
int countTo = 1;
void setup(){
pinMode(red, OUTPUT);
pinMode(amber, OUTPUT);
pinMode(green, OUTPUT);
pinMode(12, INPUT_PULLUP);
Serial.begin(9600);
randomSeed(analogRead(0)); // Initialize random seed
pushbutton.setDebounceTime(100); // set debounce time to 100 milliseconds
}
void loop(){
//Red Only
digitalWrite(red, HIGH);
delay(15000);
// Red & Amber
digitalWrite(amber, HIGH);
delay(3000);
digitalWrite(red, LOW);
digitalWrite(amber, LOW);
//green only)
digitalWrite(green, HIGH);
counter = 1;
countTo = 500; //random(1000, 30000); // end plan is to generate random number of cycles between 1000 and 10000
// This will stay on green for a random time (approx 30 seconds to 15 minutes) unless button pushed
while (counter < countTo) {
pushbutton.loop ();
Serial.print(" Counter:");
Serial.print(counter);
Serial.print (" Random Cycles:");
Serial.println (countTo);
counter = counter + 1;
if(pushbutton.isPressed()){ //when button pushed, set counter to countTo which exits while loop to continue to Amber
counter = countTo;
}
}
//Amber only
digitalWrite(green, LOW);
digitalWrite(amber, HIGH);
delay(4000);
digitalWrite(amber, LOW);
}