This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
Pi/button_led_speed.py

35 lines
854 B
Python
Raw Normal View History

2015-12-23 22:57:30 +00:00
#!/usr/bin/python
from time import sleep
import RPi.GPIO as GPIO
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
speeds = [1000, 500, 250, 150, 100, 50]
currentSpeedIndex = 0
msPassed = 0
ledOn = False
cycleMs = 50
while True:
if msPassed >= speeds[currentSpeedIndex]:
ledOn = ~ledOn
msPassed = 0
GPIO.output(4, ledOn)
buttonPressed = GPIO.input(14) == 0
if buttonPressed == True:
print 'Button pressed'
currentSpeedIndex = (currentSpeedIndex + 1) % len(speeds)
print 'New speed: ' + str(speeds[currentSpeedIndex]) + 'ms'
sleep(cycleMs / 1000.0)
msPassed += cycleMs
except KeyboardInterrupt:
GPIO.output(4, False)
finally:
GPIO.cleanup()