1. 长按按键,松手后动作
from machine import Pin
import timeEnter_key = Pin(15, Pin.IN, Pin.PULL_UP)
Enter_key_flag = 0
Enter_key_flag_temp = 0while True:if Enter_key.value() == 0:time.sleep_ms(10)while Enter_key.value() == 0:Enter_key_flag_temp = not Enter_key_flagtime.sleep_ms(100)Enter_key_flag = Enter_key_flag_temp
2.长按按键到时间动作规定次数的动作
from machine import Pin
import timeEnter_key = Pin(15, Pin.IN, Pin.PULL_UP)
Enter_key_flag = 0
Enter_key_flag_temp = 0while True:if Enter_key.value() == 0:time.sleep_ms(10)Enter_key_flag_temp = 1 # 按一次按键执行程序的次数while Enter_key.value() == 0:if Enter_key_flag_temp > 0:Enter_key_flag ^= 1Enter_key_flag_temp -= 1 # 每执行一次动作,次数减1oled_display(Enter_key_flag) # 执行一次动作time.sleep_ms(100)