0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

電子工作メモ - テーブルランプ

Last updated at Posted at 2024-03-21

モーメンタリスイッチ異なり、押すごとにON/OFFを切り替える。

  • ボタンのチャタリング
    ボタンを押したり、離したりした際に、わずかの時間電圧が不安定になる。
    そのためボタンの状態を取得するときに少し時間をおいて取得する。
from machine import Pin
import time

led = Pin(15, Pin.OUT)                   
button = Pin(13, Pin.IN, Pin.PULL_UP) # Pin13を入力として設定
def reverseGPIO():
    if led.value():
        led.value(0)                  # LED を ON
    else:
        led.value(1)                  # LED を OFF
        
try:
    while True:
        # ボタンが押されてなければ何もしない
        if not button.value():
            time.sleep_ms(20)

            # ボタンが押されたと判断してLEDの状態を変更
            if not button.value():
                reverseGPIO()
                # LED状態変更後にボタンが離されるまで待機
                while not button.value():
                    time.sleep_ms(20)
except:
    pass
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?