From c0bfa2bd7c3e985fcac1b960384d61738a1c5b42 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 23 Dec 2015 22:24:22 +0100 Subject: [PATCH] switch led on pin 4 with button on pin 14 --- button_led.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 button_led.py diff --git a/button_led.py b/button_led.py new file mode 100644 index 0000000..3093c68 --- /dev/null +++ b/button_led.py @@ -0,0 +1,26 @@ +#!/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) + + last_state = True + last_led_state = False + + while True: + input_state = GPIO.input(14) + if input_state == False and input_state != last_state: + print 'Button pressed' + GPIO.output(4, ~last_led_state) + last_led_state = ~last_led_state + last_state = input_state + sleep(0.1) +except KeyboardInterrupt: + GPIO.output(4, False) +finally: + GPIO.cleanup()