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:

symbol rot = 1
symbol gelb = 2
symbol gruen = 3
symbol fgrot = 5
symbol fggruen = 4
let w0 = 0
setint %00100000,%00100000
high fgrot
ampel:
high rot
if w0 = 1 then
gosub fussgaenger
let w0 = 0
setint %00100000,%00100000
else
pause 3000
endif
high gelb
pause 1000
low rot
low gelb
high gruen
pause 3000
low gruen
high gelb
pause 1000
low gelb
goto ampel
interrupt:
let w0 = 1
return
fussgaenger:
high fggruen
low fgrot
pause 3000
high fgrot
low fggruen
return

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.

PICAXE 20M Traffic Light Project With Pedestrian Crossing

The schematic for this circuit, drawn using tinyCAD, can be seen below.

PICAXE-20M Traffic Light Schematic

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.

symbol rot = 1
symbol gelb = 2
symbol gruen = 3
symbol fgrot = 5
symbol fggruen = 4
high fgrot
ampel:
high rot
if  pin5 = 1  then
pause 800
gosub fussgaenger
else
pause 3000
endif
high gelb
pause 1000
low rot
low gelb
high gruen
pause 3000
low gruen
high gelb
pause 1000
low gelb
goto ampel
fussgaenger:
high fggruen
low fgrot
pause 3000
high fgrot
low fggruen
pause 800
return