本站改版新增arduino频道
本实例代码方向:

按键消抖改进版
from machine import Pin
import time
import random
import time
right_key = Pin(8, Pin.OUT,Pin.PULL_UP)
down_key = Pin(9, Pin.OUT,Pin.PULL_UP)
center_key = Pin(4, Pin.OUT,Pin.PULL_UP)#上拉io4,中心按钮按下是低电平,不按的时候电平悬浮
up_key = Pin(5, Pin.OUT,Pin.PULL_UP)
left_key = Pin(13, Pin.OUT,Pin.PULL_UP)
left_key.on()
up_key.on()
right_key.on()
down_key.on()
center_key.on()
while True:
if right_key.value() == 0: #按键被按下
time.sleep_ms(10) #消除抖动
if right_key.value() == 0: #确认按键被按下
print('right_key')
while not right_key.value(): #检测按键是否松开
pass
if up_key.value() == 0: #按键被按下
time.sleep_ms(10) #消除抖动
if up_key.value() == 0: #确认按键被按下
print('up_key')
while not up_key.value(): #检测按键是否松开
pass
if down_key.value() == 0: #按键被按下
time.sleep_ms(10) #消除抖动
if down_key.value() == 0: #确认按键被按下
print('down_key')
while not down_key.value(): #检测按键是否松开
pass
if left_key.value() == 0: #按键被按下
time.sleep_ms(10) #消除抖动
if left_key.value() == 0: #确认按键被按下
print('left_key')
while not left_key.value(): #检测按键是否松开
pass
if center_key.value() == 0: #按键被按下
time.sleep_ms(10) #消除抖动
if center_key.value() == 0: #确认按键被按下
print('center_key')
while not center_key.value(): #检测按键是否松开
pass按键非消抖版
from machine import Pin
import time
import random
import time
right_key = Pin(8, Pin.OUT,Pin.PULL_UP)
down_key = Pin(9, Pin.OUT,Pin.PULL_UP)
center_key = Pin(4, Pin.OUT,Pin.PULL_UP)#上拉io4,中心按钮按下是低电平,不按的时候电平悬浮
up_key = Pin(5, Pin.OUT,Pin.PULL_UP)
left_key = Pin(13, Pin.OUT,Pin.PULL_UP)
left_key.on()
up_key.on()
right_key.on()
down_key.on()
center_key.on()
while True:
if right_key.value() == 0:
print('right_key')
if up_key.value() == 0:
print('up_key')
if down_key.value() == 0:
print('down_key')
if left_key.value() == 0:
print('left_key')
if center_key.value() == 0:
print('center_key')
time.sleep_ms(100)
# sleep for 1 second开源网址:https://github.com/jiujue/ESP32C3-air101-lcd-microPython-game
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0094609260559082 seconds