from gpiozero import LED, Button
from signal import pause
import time
# LEDとスイッチのGPIO番号
LED_PIN = 17
SWITCH_PIN = 18
MAX_RELEASE_COUNT = 3
# LEDオブジェクトを作成
led = LED(LED_PIN)
# Buttonオブジェクトを作成
button = Button(SWITCH_PIN, pull_up=True, bounce_time=0.2)
stop_flg = False
# 点滅関数
def blink_led():
# tennmetukaijoyou
global stop_flg
releaseCount = 0
while True:
for _ in range(2):
led.on()
time.sleep(0.6)
led.off()
time.sleep(0.4)
if button.is_active:
releaseCount += 1
else:
releaseCount = 0
print('releaseCount:',releaseCount)
if releaseCount >= MAX_RELEASE_COUNT:
stop_flg = True
break
return releaseCount
# スイッチが押された時の処理
def switch_pressed():
global stop_flg
print('Button_pressed')
if stop_flg == True:
print('LED OFF')
led.off()
stop_flg = False
else:
print('Call blink')
blink_led()
# スイッチが離れた時の処理
#def switch_released():
# global pressed_time
# pressed_duration = time.time() - pressed_time
# print('pressed_duration:' + str(pressed_duration))
# if pressed_duration >= 5:
# print('LED OFF')
# led.off()
# スイッチのイベントハンドラを設定
button.when_pressed = switch_pressed
#button.when_released = switch_released
try:
pause() # メインプログラムを一時停止してイベントを処理
except KeyboardInterrupt:
pass
finally:
led.close() # LEDオブジェクトをクリーンアップして終了
```
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme