-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path08_reedSwitch.py
More file actions
executable file
·37 lines (29 loc) · 964 Bytes
/
08_reedSwitch.py
File metadata and controls
executable file
·37 lines (29 loc) · 964 Bytes
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
#!/usr/bin/env python
import RPi.GPIO as GPIO
ReedPin = 11
LedPin = 12
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
GPIO.setup(ReedPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(LedPin, GPIO.LOW) # Set LedPin high(+3.3V) to off led
def switchLed(channel):
if GPIO.input(11):
print "Magnet detected - LED on!"
GPIO.output(LedPin,GPIO.HIGH)
else:
print "No magnet detected - LED off!"
GPIO.output(LedPin,GPIO.LOW)
def loop():
GPIO.add_event_detect(ReedPin,GPIO.BOTH, callback=switchLed, bouncetime=20)
while True:
pass
def destroy():
GPIO.output(LedPin, GPIO.LOW) # led off
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()