I hope this isn't as silly as the topic sounds.
I'm working on Project 4 in 'Beginning Arduino' by Michael McRoberts. I didn't like his UK style traffic lights with pedestrian button, so I decided to clean it up and change it to US style. Especially since his pedestrian call button didn't seem to work.
The first thing I found was another 'new breadboard' problem. This board is long enough so they split it. In other words you could have separate Hi and Lo voltage buses for each of the 4 sections of a board. It's been a long time since I put one of these into service and I forgot the jumpers. No wonder the pedestrian call button didn't work. All the parts were connected to a power bus with no power on it, or ground. All they were getting was the signals from the Arduino.
Now the real question. The only push button I have handy for the pedestrian call is a momentary, which is what I would expect to find on a traffic light anyway. How do I capture the button push? It's connected to Digital I/O Pin 2, but only raises that pin to a high while it is pushed. If I was using an event-driven programming language, or even ladder logic, trapping it would be easy, but . . . . how do you do it in Arduino? Or do I just need to check the value at the end of each of the three delays in the lighting cycle?
This is the code I am using for testing at this point:
// Project 4 - Traffic Light with Pedestrian Walk Light
// US Style
int carRed = 10;
int carYellow = 9;
int carGreen = 8;
int pedRed = 7;
int pedGreen = 6;
int button = 2;
int state = 0; // Button press state.
int crossTime = 5000;
unsigned long changeTime; // Time since button press.
void setup()
{
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT);
// Set up to start with pedRed ON.
digitalWrite(pedRed, HIGH);
}
void loop()
{
// Call the function to change the lights.
cycleLights();
}
// The function that manipulates the lights.
void cycleLights()
{
digitalWrite(carRed, LOW);
digitalWrite(carGreen, HIGH);
delay(10000);
digitalWrite(carGreen, LOW); // Green to OFF
digitalWrite(carYellow, HIGH); // Yellow to ON.
delay(3000); // Hold for 3 seconds.
digitalWrite(carYellow, LOW); // Yellow to OFF
digitalWrite(carRed, HIGH); // Red to ON.
delay(10000); // Hold for 10 second.
int state = digitalRead(button);
if ((state == HIGH) && ((millis() - changeTime) > 5000))
{
digitalWrite(pedGreen, HIGH);
delay(1000);
digitalWrite(pedGreen, LOW);
}
// Loop
}
McRoberts will probably get to it later in the book, but . . .