from machine import Pin, PWM
from time import ticks_ms, ticks_diff, sleep
# Setup
button = Pin(16, Pin.IN)
buzzer = PWM(Pin(13))
while True:
if button.value() == 1: # Button pressed
start_time = ticks_ms()
# Monitor until button released
while button.value() == 1:
elapsed = ticks_diff(ticks_ms(), start_time)
print("Button pressed for", elapsed, "ms.", end="\r")
sleep(0.1) # Smaller delay for smoother response
final_duration = ticks_diff(ticks_ms(), start_time)
print("\nFinal duration:", final_duration, "ms")
from machine import Pin, PWM
from time import ticks_ms, ticks_diff, sleep
# Setup
button = Pin(16, Pin.IN)
buzzer = PWM(Pin(13))
while True:
if button.value() == 1: # Button pressed
start_time = ticks_ms()
# Monitor until button released
while button.value() == 1:
elapsed = ticks_diff(ticks_ms(), start_time)
print("Button pressed for", elapsed, "ms.", end="\r")
sleep(0.1) # Smaller delay for smoother response
final_duration = ticks_diff(ticks_ms(), start_time)
print("\nFinal duration:", final_duration, "ms")