Sunday, May 28, 2017

Byte: Raspberry Picraft Weekend - Challenge 3



Challenge 3:  Push button to set a pedestrian crossing time delay

Time to try something different: another real world application involving buttons and lights is a pedestrian crossing. We’re going to start with the basics of where we ended Challenge 1. Your code should look like this:

#import necessary libraries
from gpiozero import LED, Button
from time import sleep

#define variables
red = LED(17)
amber = LED(18)
green = LED(23)
blue = LED(24)
button1 = Button(5)
button2 = Button(6)
button3 = Button(12)
button4 = Button(13)

With this code, your Pi can talk to your LEDs and push buttons, as well as set delays between interpreting or sending signals to or from them. This time, rather than use each button to light a single LED, this time, we’re going to use each button to light each LED in sequence, but after a different amount of time, to represent the various time delays you get at pedestrian crossings with varying amounts of traffic (Busier junctions tend to prioritise vehicles, rather than pedestrians, in order to prevent traffic jams).

We’ll worry about how to use the buttons in a little while, but first we should code a generic traffic light sequence to test that everything is still working. Even though we have an extra blue LED that’s not in the standard traffic light sequence, we might as well include it in our sequence, because we can use it with the toggle function to help identify which button has been pressed to tell the user how long they long is safe to cross the road. Add the following code into the while True: condition statement and give it a test.

#import necessary libraries
from gpiozero import LED, Buzzer
from time import sleep

#define variables
red = LED(17)
amber = LED(18)
green = LED(23)
blue = LED(24)
button1 = Button(5)
button2 = Button(6)
button3 = Button(12)
button4 = Button(13)
while True:  
                red.on()
                sleep(1.5)
                red.off()
                sleep(0.1)
                amber.on()
                sleep(0.5)
                amber.off()
                sleep(0.1)
                green.on()
                sleep(3.5)
                green.off()
                sleep(0.1)
                blue.toggle()

You can, of course, alter the sleep commands to be more representative of traffic light sequences that you know on your own route to work, such as the one that makes you wait for FIVE MINUTES on red before letting through cars on your road, FOR ALL OF TWENTY SECONDS, even though YOU’RE on the BUSIER route into the junction... What’s that? What do you mean? No... no, I’m not bitter about it... NOT AT ALL... WHAT MAKES YOU THINK THAT? HUH?! WELL?!?

Hooooo... Deep breath, now... It’s time to get the buttons involved. We’re going to combine sleep commands with button presses to set the time delays used by the LEDs to make them flash. To do this, we’re going to need an if/elif statement, rather the like the one written below:

#import necessary libraries
from gpiozero import LED, Buzzer
from time import sleep

#define variables
red = LED(17)
amber = LED(18)
green = LED(23)
blue = LED(24)
button1 = Button(5)
button2 = Button(6)
button3 = Button(12)
button4 = Button(13)
while True:  
     #poll for input from the buttons
    if button1.is_pressed:
        #rapid flash – 0.1s
        red.on()
        sleep(0.1)
        red.off()
        sleep(0.1)
        amber.on()
        sleep(0.1)
        amber.off()
        sleep(0.1)
        green.on()
        sleep(0.1)
        green.off()
       sleep(0.1)
       blue.on()
       sleep(0.1)
       blue.off()
elif button2.is_pressed:
        #quick flash – 0.3s
        red.on()
        sleep(0.3)
        red.off()
        sleep(0.1)
        amber.on()
        sleep(0.3)
        amber.off()
        sleep(0.1)
        green.on()
        sleep(0.3)
        green.off()
       sleep(0.1)
       blue.on()
       sleep(0.3)
       blue.off()
elif button3.is_pressed:
        #moderate flash – 0.7s
       red.on()
        sleep(0.7)
        red.off()
        sleep(0.1)
        amber.on()
        sleep(0.7)
        amber.off()
        sleep(0.1)
        green.on()
        sleep(0.7)
        green.off()
       sleep(0.1)
       blue.on()
       sleep(0.7)
       blue.off()
elif button4.is_pressed:
        #slow flash – 1.5s
        red.on()
        sleep(1.5)
        red.off()
        sleep(0.1)
        amber.on()
        sleep(1.5)
        amber.off()
        sleep(0.1)
        green.on()
        sleep(1.5)
        green.off()
       sleep(0.1)
       blue.on()
       sleep(1.5)
       blue.off()

This while True loop, which allows us to use push buttons to set time delays, is going to come in useful very soon, when we introduce our final component, the PiCamera module. But before we get to that, there are two code constructs that we still haven’t used that are essential for serious Python programming: generating random numbers and using For loops. We will look at integrating those functions into our next LED sequencing challenge. Stay tuned, Python Pilgrims!
Post a Comment