Thanks to Keith Wright for diligently testing it and pointing out bugs for me – fixed some issues in the gpio command, and while I was at it, I added in 2 new functions to it to allow for easy exporting/unexporting of the /sys/class/gpio interface – so now you can write a little script to do the exports for you, then run your own program that uses that interface (e.g.) a Python, shell, or php script, then un-export them afterwards, all without needing to be root (or use the sudo command)
Hi Gordon
I’m a novice and I’m all fired up with enthusiasm for the RasPi, and your projects so far have made it a great experience. I actually managed to get the tuxx.sh Pelican Crossing project working. I am also learning Python, so it would be really good if you showed us how to write that program in Python. Keep up the good work and kind regards
KC
Hi,
Glad you’r enjoying it so-far.
However.. I’m not a Python programmer… I’ve written (actually modified) just one Python program in my whole life. Everything I do these days is in C or BASIC (or php, sometimes perl & shell).
There is a Python wrapper for wiringPi though, so I might have a look at it all sometime – you never know!
-Gordon
Hi again Gordon
I have managed to get a Python version working, and the listing is below. It uses RPi.GPIO. Compared with your shell version (tuxx.sh) my version uses a lot of CPU while waiting for the button to be pressed. Maybe some of your readers will have suggestions for improving this code.
Cheers
Kieran
#Python program for operating the Pelican Crossing breadboard circuit by
# Kieran Cranley. 1-Aug-12
from time import sleep
import RPi.GPIO as GPIO
# **NB these are NOT wiringPi pin nos!**
GPIO.setup (3, GPIO.IN) # button switch Pin #3
GPIO.setup (11, GPIO.OUT) # red LED Pin #11
GPIO.setup (12, GPIO.OUT) # amber LED Pin #12
GPIO.setup (13, GPIO.OUT) # green LED Pin #13
GPIO.setup (15, GPIO.OUT) # redman LED Pin #15
GPIO.setup (16, GPIO.OUT) # greenman LED Pin #16
print “Waiting for button…”
def waitButton():
if GPIO.input(3):
GPIO.output(13, True) # green LED ON
GPIO.output(11, False) # red LED OFF
GPIO.output(15, True) # redman LED ON
else:
print ” Button has been pressed”
GPIO.output(13, False) # green LED OFF
GPIO.output(12, True) # amber LED ON
sleep(1)
GPIO.output(12, False) # amber LED OFF
GPIO.output(11, True) # red LED ON
sleep(1)
stopTraffic();
walk();
sleep(3)
graceTime();
startTraffic();
def stopTraffic():
print ” Stop traffic”
GPIO.output (13, False) # green LED OFF
GPIO.output (15, False) # redman LED OFF
GPIO.output (11, True) # red LED ON
def walk():
print ” Walk”
GPIO.output (15, False) # redman LED OFF
GPIO.output (16, True) # greenman LED ON
sleep(3)
def graceTime():
print ” Flashing”
GPIO.output (11, False) # red LED OFF
for i in range (1, 8):
GPIO.output (16, False) # greenman LED OFF
GPIO.output (12, False) # amber LED OFF
sleep(0.5)
GPIO.output (12, True) # amber LED ON
GPIO.output (16, True) # greenman LED ON
sleep(0.5)
GPIO.output (16, False) # greenman LED OFF
GPIO.output (12, False) # amber LED OFF
def startTraffic():
print ” Don’t walk!”
sleep(0.5)
GPIO.output (15, True) # redman LED ON
GPIO.output (13, True) # green LED ON
print “\nWaiting for button…”
while 1:
waitButton();
”’def main(): # The program works
return 0 # whether this code
# is included or not
if __name__ == ‘__main__’:
main()”’
That’s great!
I suspect your comment isn’t going to come out indented the way Python likes it though – I’ll blame this theme I’m using in WordPress….
Basically, I do a sleep 0.1 in my shell script while waiting on the button – it does mean that a very brief stab at the button might be missed though, but it reduces CPU usage down… It’s not a perfect way of doing it, but it mostly works!
-Gordon
That works a treat! Now there is no visible CPU activity.
Cheers
Kieran
OK Gordon – now that I’ve got this running with RPi.GPIO, using
import RPi.GPIO as GPIO
GPIO.setup (3, GPIO.IN)
GPIO.setup (11, GPIO.OUT) etc etc
what do I need to replace these with to use wiringPi?
Kieran
what I’d suggest is this:
Use the gpio command to setup the pins – like:
gpio export 3 in
gpio export 11 out
etc.
then in your python script, remove (or comment out) the GPIO.setup statements, then you should be able to run your program without using sudo.
wiringPi and the existing RPi.GPIO library are essentially doing the same thing, but you can use the gpio command that comes with wiringPi as an easy way to initialise the GPIO pins + direction without the need to be root, or use sudo.
-Gordon