-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_test.py
More file actions
60 lines (48 loc) · 1.53 KB
/
button_test.py
File metadata and controls
60 lines (48 loc) · 1.53 KB
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
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
green_button = 13
red_button = 6
red_led = 12
green_led = 5
GPIO.setup(red_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(green_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(green_led, GPIO.OUT)
red_input_state = GPIO.input(red_button) # Sense the button
green_input_state = GPIO.input(green_button) # Sense the button
print ("red is currently %d" %red_input_state)
print ("green is currently %d" %green_input_state)
GPIO.output(red_led, 1)
GPIO.output(green_led, 1)
if (green_input_state == False):
green_invert = True
else:
green_invert = False
if (red_input_state == False):
red_invert = True
else:
red_invert = False
print ("red invert is currently %d" %red_invert)
print ("green invert is currently %d" %green_invert)
while True:
time.sleep(0.2)
red_input_state = GPIO.input(red_button) # Sense the button
if red_input_state == red_invert:
print('RED Button Pressed')
time.sleep(0.2)
# Switch on LED
GPIO.output(red_led, 1)
else:
# Switch off LED
GPIO.output(red_led, 0)
green_input_state = GPIO.input(green_button) # Sense the button
if green_input_state == green_invert:
print('GREEN Button Pressed')
time.sleep(0.2)
# Switch on LED
GPIO.output(green_led, 1)
else:
# Switch off LED
GPIO.output(green_led, 0)