pigpioでswを使ってLEDを交互にonoffを
配線図
3.3V — 200オーム — LED — GPIO17
3.3V — 10kオーム ——— |sw|——–GND
|————- GPIO18
押せばGPIO18 離せばGND
割り込み実行の受け渡しなんだけど、
pi.callback(PIN, pigpio.FALLING_EDGE, cb_interrupt)がお約束なので
globalで再定義してフラグを使用
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pigpio
import time
LedPin = 17
PIN=18
pi = pigpio.pi()
Led_status = True
def setup():
pi.set_mode(PIN, pigpio.INPUT)
pi.set_mode(LedPin, pigpio.OUTPUT)
pi.write(LedPin, pigpio.HIGH)
def cb_interrupt(gpio, level, tick):
global Led_status
print(tick)
if Led_status==True:
pi.write(LedPin, pigpio.HIGH)
print('LED OFF')
Led_status = False
else:
pi.write(LedPin, pigpio.LOW)
print('LED ON')
Led_status = True
def main():
cb = pi.callback(PIN, pigpio.FALLING_EDGE, cb_interrupt)
while True:
time.sleep(1)
def destroy():
pi.write(LedPin, pigpio.HIGH)
pi.stop()
if __name__ == '__main__':
setup()
try:
main()
except KeyboardInterrupt:
destroy()