button switches led speed
This commit is contained in:
parent
20a35b764e
commit
71b29ed3d5
34
button_led_speed.py
Executable file
34
button_led_speed.py
Executable file
@ -0,0 +1,34 @@
|
||||
#!/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()
|
Reference in New Issue
Block a user