0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Lチカ

Posted at

RaspberryPiでLEDを点滅させる。

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()

スイッチのイベントハンドラを設定

button.when_pressed = switch_pressed
#button.when_released = switch_released

try:
pause() # メインプログラムを一時停止してイベントを処理

except KeyboardInterrupt:
pass

finally:
led.close() # LEDオブジェクトをクリーンアップして終了

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?