Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython 合宙esp32c3获取air101-lcd扩展板屏幕方向键按键


本实例代码方向:

1657805084928346.jpg

按键消抖改进版

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

执行时间: 0.01023006439209 seconds