Pi/Moped/moped.py

46 lines
1023 B
Python
Raw Normal View History

2016-01-07 12:59:27 +00:00
#!/usr/bin/python
from time import sleep
import RPi.GPIO as GPIO
yellowLed = 15
redLed = 17
greenLed = 18
buttonPin = 27
2016-01-07 14:59:21 +00:00
sleepTimes = [1, 0.5, 0.25, 0.125]
sleepTimeIndex = 0
2016-01-07 12:59:27 +00:00
def buttonHandler(channel):
2016-01-07 14:59:21 +00:00
global sleepTimeIndex
2016-01-07 12:59:27 +00:00
print 'button pushed'
2016-01-07 14:59:21 +00:00
sleepTimeIndex = (sleepTimeIndex + 1) % len(sleepTimes)
2016-01-07 12:59:27 +00:00
2016-01-07 14:59:21 +00:00
def setLeds(yellow, red, green):
GPIO.output(yellowLed, yellow)
GPIO.output(redLed, red)
GPIO.output(greenLed, green)
2016-01-07 12:59:27 +00:00
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(yellowLed, GPIO.OUT)
GPIO.setup(redLed, GPIO.OUT)
GPIO.setup(greenLed, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000)
while True:
2016-01-07 14:59:21 +00:00
setLeds(1, 0, 0)
sleep(sleepTimes[sleepTimeIndex])
setLeds(0, 1, 0)
sleep(sleepTimes[sleepTimeIndex])
setLeds(0, 0, 1)
sleep(sleepTimes[sleepTimeIndex])
2016-01-07 12:59:27 +00:00
finally:
2016-01-07 14:59:21 +00:00
setLeds(0, 0, 0)
2016-01-07 12:59:27 +00:00
GPIO.cleanup()