esp32 micropython引脚电容值实现模拟按键

from machine import TouchPad, Pin  #引入touchpad模块
from time import sleep
import utime

touch_up = TouchPad(Pin(12))   #12是上
touch_down = TouchPad(Pin(13)) #13是下
num = 1
last = 0
start = utime.ticks_ms()
while True:
    touch_up_value = touch_up.read()
    touch_down_value = touch_down.read()
    print(num)   

    if touch_up_value < 500 :      #若读取的值大于阀值,则为没触摸
        if touch_down_value > 500 and last <=0:   #按上没按下
           num = num + 1
           last = last + 1
           start = utime.ticks_ms()
    elif touch_down_value < 500 and last != -1:
       num= num -1
       last = -1
       start = utime.ticks_ms()
    end = utime.ticks_ms()
    if end - start > 500:#last每过一段时间要恢复为0
        last = 0


    sleep(0.1)                 #延时0.1秒,实现每0.1秒判断一次触摸状态

引脚12实现对num的加操作,引脚13实现对num的减操作

posted @ 2022-11-08 16:50  李里力离  阅读(319)  评论(0)    收藏  举报