From 71b29ed3d5219d1fd3921d2b0f3577f719f3a114 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 23 Dec 2015 23:57:30 +0100 Subject: [PATCH] button switches led speed --- button_led_speed.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 button_led_speed.py 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()