diff --git a/button_led_speed.py b/button_led_speed.py new file mode 100755 index 0000000..04b7ee9 --- /dev/null +++ b/button_led_speed.py @@ -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()