State Change Detection example script (phyduino.py) Counts presses of a pushbutton on D2 and lights the on-board LED on every fourth press. It acts on the change from released to pressed (an edge), not on the button being held down.
- Wiring
- A pushbutton between D2 and 3.3 V; the pull-down holds D2 at 0 while the button is open.
- Output
on
number of button pushes: 1
off
import time
import phyduino as mcu
button = mcu.pin("D2", "in", pull="down")
mcu.led.off()
presses = 0
last_state = 0
while True:
state = button.value()
if state != last_state:
if state == 1:
presses += 1
print("on")
print("number of button pushes:", presses)
else:
print("off")
time.sleep(0.05)
last_state = state
mcu.led.value(presses % 4 == 0)