In a previous post describing how to build a traffic light (or stop light) system using a PICAXE microcontroller, I did not include the details of how to implement a pedestrian crossing part of the project. This post will describe the two ways we tried to implement the feature. Our first attempt used the interrupt feature of the PIXACE 20M and a push button connected to +5v and input pin 5 on the microcontroller. By using the setint instruction, the PICAXE can detect changes on its input pins and match given states, for example: setint %00000000, %00100000 will trigger and interrupt when pin 7 is low and setint %00100000, %00100000 will trigger and interrupt when pin 7 is high. Once an interrupt has been activated, the PICAXE does a gosub to the subroutine at label interrupt and returns to the place it left from on completion. Below is the code we implemented for the first version of the pedestrian crossing using the interrupts described:
Unfortunately, this design has a pretty big issue. Namely, when the push button is pressed, the interrupt breaks out of pause statements and does not return to within the pause but immediately after it. This means that a pedestrian can influence the speed at which the traffic light system switches from one phase to another. To get around this, we decided to replace the use of interrupts by a hardware flag in the form of a latch built from an AND gate and a diode. When the user presses the pedestrian crossing button the AND gate latches the input and during the red phase, a check is made to see if the button was pressed. If it was, the pedestrian light sequence is started and the program loops around as normal. In the picture below, you will be able to see the AND gate latch, the traffic light LEDs and the pedestrian crossing LEDs.
The schematic for this circuit, drawn using tinyCAD, can be seen below.
Below is the final piece of code for the hardware shown above. There are of course, improvements that could be made, but for the time being, I think that this was a good first step into the PICAXE world.