-
Notifications
You must be signed in to change notification settings - Fork 0
/
button_demo.py
64 lines (59 loc) · 1.71 KB
/
button_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# Added Tkinter interface Chuck Duey 8/20/2015
# Dual LED 9/24/2016 Chuck Duey
# Changed to Relay toggle rate
# Import the modules
import RPi.GPIO as GPIO
import time
from Tkinter import *
# Define our statics
ON = 1
OFF = 0
# Define our pin usage:
button1 = 24
button2 = 25
led1 = 4
led2 = 23
# Set blink rate for global in ms and blank state
led_blink_rate = 600
led_state = OFF
#Set up the GPIO ports
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(button1, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(button2, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
def led_blink():
global led_state,led_blink_rate,led1,led2
if led_state == ON:
led_state = OFF
else:
led_state = ON
GPIO.output(led1,led_state)
GPIO.output(led2,1-led_state)
clock.after(led_blink_rate/2,led_blink)
def blinkChange(channel):
global led_blink_rate,clock
if channel == button1:
if led_blink_rate > 50:
led_blink_rate -= 50
if channel == button2:
if led_blink_rate < 2000:
led_blink_rate += 100
clock.config(text="Relay Toggle Rate = "+str(led_blink_rate)+" ms")
# Define Tk root
root = Tk ()
root.title("Button Demo")
clock = Label(root, font=('times', 20, 'bold'),bg='green')
clock.pack(fill=BOTH, expand=1)
clock.config(text="Relay Toggle Rate = "+str(led_blink_rate)+" ms")
# Main
# Set up events
GPIO.add_event_detect(button1, GPIO.RISING, callback=blinkChange, bouncetime=300)
GPIO.add_event_detect(button2, GPIO.RISING, callback=blinkChange, bouncetime=300)
clock.after(led_blink_rate,led_blink)
# Start main loop
root.mainloop( )
# Clean up IO ports return to normal
GPIO.cleanup()