Stepping up from traffic lights last time, I decided this time to have a crack at making a Pelican crossing.

A Pelican crossing is a pedestrian crossing consisting of two sets of traffic lights, a button, and a signal to indicate that it is safe for pedestrians to cross. The sequence that the lights follow is slightly more complex than before:

  • Initially, the traffic signal lights are green and the pedestrian “red man” is on red.
  • When the button is pressed, the traffic signal lights cycle to red.
  • Once the traffic signals are red, the pedestrian signal is set to green and a buzzer sounds for a period of time.
  • When the buzzer has finished beeping, the traffic signals are set on flashing amber and the pedestrian green signal also flashes.
  • After a little while, the traffic signal is reset to green and the pedestrian signal to red.

The Circuit

The circuit for this is slightly more complex.

The traffic signals are now linked, so each red, amber, and green light can be linked in parallel. We introduce two new lights for the pedestrian signal, together with a buzzer.

experiment 2 - traffic lights crossing

I know there are two red/green men in real life, but this is a slight simplification. Just connect red/green leds in parallel for the second set, as we did for the main linked traffic signals.

The Software

The code is fairly similar to before, we extend the class to control three additional outputs – the red and green man and a buzzer, as well as to listen to a given input button press.

The main loop waits for a button press and when detected it toggles the lights, sounds the buzzer, then toggles the lights back.

import piface.pfio
import time
import sys
class crossinglight:
""" A (UK) Crossing traffic light """
def __init__(self, red_pin, amber_pin, green_pin, red_man_pin, green_man_pin, beeper, button):
self.red_pin = red_pin
self.amber_pin = amber_pin
self.green_pin = green_pin
self.red_man_pin = red_man_pin
self.green_man_pin = green_man_pin
self.beeper = beeper
self.button = button
self.status = 'green'
piface.pfio.digital_write(self.green_pin, 1)
piface.pfio.digital_write(self.red_man_pin, 1)
def beep(self):
for cnt in range(1, 10):
piface.pfio.digital_write(self.beeper, 1)
time.sleep(1)
piface.pfio.digital_write(self.beeper, 0)
time.sleep(1)
def wait_for_button(self):
pressed = False;
while pressed == False:
if piface.pfio.digital_read(self.button) == 1:
pressed = True;
def toggle(self):
cnt = 0;
if self.status == 'red':
piface.pfio.digital_write(self.red_pin, 0)
# Transitioning from red to green, blink amber light and green man
for cnt in range(1, 5):
piface.pfio.digital_write(self.amber_pin, 1)
piface.pfio.digital_write(self.green_man_pin, 1)
time.sleep(1)
piface.pfio.digital_write(self.amber_pin, 0)
piface.pfio.digital_write(self.green_man_pin, 0)
time.sleep(1)
piface.pfio.digital_write(self.amber_pin, 0)
piface.pfio.digital_write(self.red_pin, 0)
piface.pfio.digital_write(self.green_pin, 1)
piface.pfio.digital_write(self.green_man_pin, 0)
piface.pfio.digital_write(self.red_man_pin, 1)
self.status = 'green'
else:
piface.pfio.digital_write(self.green_pin, 0)
piface.pfio.digital_write(self.amber_pin, 1)
time.sleep(2)
piface.pfio.digital_write(self.amber_pin, 0)
piface.pfio.digital_write(self.red_pin, 1)
piface.pfio.digital_write(self.green_man_pin, 1)
piface.pfio.digital_write(self.red_man_pin, 0)
self.status = 'red'
def main(argv):
piface.pfio.init()
# initialise our light
light = crossinglight(0,1,2, 3,4, 5, 0)
# Loop, waiting for button press (probably could be done better with interrupts)
while True:
light.wait_for_button();
light.toggle()
light.beep()
light.toggle()
if __name__ == "__main__":
sys.exit(main(sys.argv))

Here it is in action…


Thanks for visiting! If you found this helpful, you might be interested in knowing that I offer development and consultancy services, and am available to hire!

Leave a Reply