I'm trying to work out the logic for an Arduino project that I am doing.
This is a remix of the classic traffic light problem with two pushbuttons for pedestrian crossing. Basically what it should do is:
- green light for 10 seconds
- yellow light for 2 seconds
- red light for 5 seconds with solid blue crossing lights
- red light for 5 seconds with flashing blue crossing lights warning pedestrians to hurry
- at any time one of two push buttons (either side of the street) can be used to tell the program that pedestrians are going to cross
The light change bit I have working, but the crossing sequence is giving me trouble.
The pushbuttons are connected to digital pins 2 and 3 and use interrupts to start the crossing sequence, which looks like this.
a) if the light is green, delay for 2 seconds for traffic, then turn yellow and red
b) if the light is yellow, do nothing since the crossing sequence is about to start anyway
c) if the light is red, restart the red sequence to give pedestrians more time to cross
The problem that I am having is this: when using interrupts the code will return to the same place where it was before the interrupt happened after the interrupt code finished. This means that if you pressed the interrupt during the green cycle, it would go to the yellow and red cycles, then return to the same spot and go right back to yellow and red. I would like it to go back to the beginning of the code and restart the green cycle after the crossing cycle is finished.
From what I've read, the goto statement is evil and wouldn't work anyway since the interrupt function is outside of the scope of the main loop. Any help with the logic/ programming structures would be very helpful!
trafficLight.ino (1.71 KB)