テストに使用するプログラム──磁気を検知したら,LED が点灯
#!/usr/bin/python
import RPi.GPIO as GPIO
SignalPin = 18
LedPin = 16
def setup():
# Set the GPIO pins as numbering
GPIO.setmode(GPIO.BOARD)
# Set the SignalPin's mode is input, and (Detected→) ON→ LOW
GPIO.setup(SignalPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Set the LedPin's mode is output
GPIO.setup(LedPin,GPIO.OUT)
# Set the LedPin low
GPIO.output(LedPin, GPIO.LOW)
def destroy():
# Release resource
GPIO.cleanup()
def loop():
while True:
if GPIO.input(SignalPin) == GPIO.LOW:
GPIO.output(LedPin,GPIO.HIGH)
else:
GPIO.output(LedPin,GPIO.LOW)
# The Program will start from here
if __name__ == '__main__':
setup()
try:
loop()
# When control c is pressed child program destroy() will be executed.
except KeyboardInterrupt:
destroy()
|
$ vi ky_003.py
$ chmod +x ky_003.py
$ ./ky_003.py
|