Ceci est une ancienne révision du document !
Welcome back to our Real World programming series. Last time, we programmed the RPi to turn on and off an LED when a button was pressed. Very simple, but this got us started. This month, we will do another simple project, a traffic light simulator using 3 LEDs, one Red, one Yellow and one Green. For the most part, the code is very similar to what we used last month, so you shouldn’t have any problems. If you have any questions, I suggest you look at last month’s article which should answer any of your concerns.
First, let’s look at the schematic and the breadboard (below right).
Notice that the wire colours correspond to its ‘job’, with the exception of the orange wire. The red wires supply 3.3 volts. The green wire controls the green LED, the yellow wire controls the yellow LED, and the orange wire controls the red LED, since the red wire is already being used.
You should also know that the pins being used should work on a RPi v1a/b, RPi v1b+ and RPi v2b.
The red LED cathode is connected to GPIO 17 (Physical pin 11), yellow LED cathode is connected to GPIO 23 (Physical pin 16), and the green LED cathode is connected to GPIO 22 (Physical pin 15). The Anodes of all three LEDs are connected to one side of 220 Ohm resistors and the other sides are connected to a common 3.3 VDC. We don’t need the ground voltage for this particular project.
Since I’ve driven only in the U.S. I’ve based the simulation on our traffic patterns. Long red light (10 seconds), green light is usually shorter than the red light time (8 seconds), and the yellow light is fairly short (2 seconds). These values are currently hard coded in the time.sleep() function calls. Feel free to change them as you see fit.
Now let’s start working through the code.
#!/usr/bin/env python
# Traffic Light Simulator # Written by G. D. Walters
#————————–
import RPi.GPIO as GPIO import os import time import datetime
#————————–
RedLedPin = 17 YellowLedPin = 23 GreenLedPin = 22
The first 9 lines are our standard import statements and a few comment lines. The next three lines define the BCM pin numbers for our LED pins. If you wish to use physical pin numbers, be sure to change the GPIO.setmode() line in the next routine (top right).
As I mentioned above, the GPIO.setmode needs to be changed from ‘GPIO.BCM’ to ‘GPIO.BOARD’ if you want to use the physical pin numbers instead of the BCM numbers in our definitions. The next three lines set the LED pins as output pins, and then turn all three LEDs off to start the program by setting the output value to HIGH.
def LEDLoop():
print "Green On..." GPIO.output(GreenLedPin,0) time.sleep(8) GPIO.output(GreenLedPin,1) print "Green Off..." print "Yellow On..." GPIO.output(YellowLedPin,0) time.sleep(2) GPIO.output(YellowLedPin,1) print "Yellow Off..." print"Red On..." GPIO.output(RedLedPin, 0) time.sleep(10) GPIO.output(RedLedPin,1) print "Red Off..."
The LEDLoop routine is very simple: • We print on the console “<color> On…”,# • Turn the LED on by setting the output value to 0 or low, • Sleep for a designated period, • Set the output value of the pin back to 1 or high, • Then print that the LED is now off.
This is then duplicated for the Yellow and Red LEDs. The loop() routine simply forces the LEDLoop() routine to be called over and over until the user hits <CTRL> C on the RPi keyboard.
def loop():
while True: LEDLoop()
The destroy routine and the main loop are the same as last month. We simply set all the LED pins to high, turning them off, and then call GPIO.cleanup().
I’m not sure that we could make a much simpler program to do what we need to do.
If you want, you could duplicate the 3 LEDS and make an intersection simulation before next time.
Next time, we’ll have something that is a bit more challenging. Until then, happy programming.
ENCART APP
The Official Full Circle App fot Ubuntu Touch
Brian Douglass has created a fantastic app for Ubuntu Touch devices that will allow you to view current issues, and back issues, and to download and view them on your Ubuntu Touch phone/tablet.
Install
Either search for 'full circle' in the Ubuntu Touch store and click install, or view the URL below on your device and click install to be taken to the store page.
https://uappexplorer.com/app/ fullcircle.bhdouglass