自适应 网站开发,二次开发招聘,百度搜索流量查询,宁波网站推广建站Arduino OneButton按键处理库实现单击/双击长按功能 ✨在Arduino开发平台下#xff0c;按键的单击/双击/长按功能#xff0c;在通过使用OneButton库#xff0c;很容易就可以轻松实现。这就是支持C/C模块化设计的好处#xff0c;避免重复性开发的工作。 #x1f516;本文将… Arduino OneButton按键处理库实现单击/双击长按功能 ✨在Arduino开发平台下按键的单击/双击/长按功能在通过使用OneButton库很容易就可以轻松实现。这就是支持C/C模块化设计的好处避免重复性开发的工作。 本文将具体接收OneButton库的相关调用函数介绍说明以及有关常用按键操作的使用方法。 OneButton库github地址https://github.com/mathertel/OneButton
OneButton接口函数 OneButton(const int pin, const boolean activeLow true, const bool pullupActive true);//实例化OneButton对象 pin,必填参数指定引脚号。bool类型可选默认参数是true:按下为低电平; false : 按下为高电平bool,类型可选默认参数是true,也就是将引脚上拉开启。 setClickTicks(const unsigned int ms)设置单击时间setDebounceMs(const unsigned int ms)设置双击时间setPressTicks(const unsigned int ms)设置长按时间attachClick(callbackFunction newFunction);单击时调用的函数。attachDoubleClick(callbackFunction newFunction);双击时调用的函数。attachMultiClick(callbackFunction newFunction);多次按此单击时调用的函数。attachLongPressStart(callbackFunction newFunction);长按开始时调用的函数。attachLongPressStop(callbackFunction newFunction);长按结束调用的函数。attachDuringLongPress(callbackFunction newFunction);长按期间调用的函数。tick(void);按键扫描函数。tick(bool level);重新给按键引脚电平状态。reset(void);重启按键状态。getNumberClicks(void);获取按键次数单击或多击。bool isIdle() 查询当前按键状态。如果当前正在处理按钮按流则返回true。(这允许对电源敏感的应用程序知道何时可以安全地关闭主CPU)isLongPressed()当检测到长按时为True
测试代码
测试对象ESP32S3 单击按下esp32板上的 boot0按键时板载ws2812变为红色双击按钮变成绿色长按之后变成蓝色。 #include Arduino.h
#include OneButton.h //https://github.com/mathertel/OneButton
#include FastLED.h //https://github.com/FastLED/FastLED#define KEY 0 //esp32 BOOT0按键引脚#define LED_PIN 48 //ESP32-S3-DevKitC-1 RGB GPIO38 /YD:GPIO48
#define NUM_LEDS 1
OneButton button(KEY, true);CRGB leds[NUM_LEDS];void click();/******单击******/
void doubleclick();/******双击******/
void longPressStart();/******长按开始******/
void duringLongPress();/******长按期间******/
void longPressStop();/******长按结束******/
void attachPressStart();void setup()
{Serial.begin(115200);FastLED.addLedsWS2812, LED_PIN, GRB(leds, NUM_LEDS);// pinMode(RGB_PIN,OUTPUT);// digitalWrite(RGB_PIN,LOW);button.reset();//清除按钮状态机的状态button.attachClick(click);//注册单击button.attachDoubleClick(doubleclick);//注册双击button.attachLongPressStart(longPressStart);//注册长按开始button.attachDuringLongPress(duringLongPress);//注册长按button.attachLongPressStop(longPressStop);//注册长按结束button.attachDuringLongPress(attachPressStart);//按下键就会持续触发leds[0] CRGB(255, 0, 0); // 设置颜色为红色FastLED.show();delay(1000);leds[0] CRGB(0, 255, 0); // 设置颜色为绿色FastLED.show();delay(1000);leds[0] CRGB(0, 0, 255); // 设置颜色为红色FastLED.show();delay(1000);leds[0] CRGB(0, 0, 0); // 关闭FastLED.show();
}
void loop()
{button.tick();delay(10);
}
/******单击******/
void click()
{Serial.println(click);leds[0] CRGB(255, 0, 0); // 设置颜色为红色FastLED.show();
}
/******双击******/
void doubleclick()
{Serial.println(Doubleclick);leds[0] CRGB(0, 255, 0); // 设置颜色为绿色FastLED.show();
}
/******长按开始******/
void longPressStart()
{Serial.println(LongPressStart);
}
/******长按期间******/
void duringLongPress()
{if (button.isLongPressed()){Serial.printf(DuringLongPress,KEY STATE:%d\r\n,digitalRead(KEY));delay(50);//稍作延时处}
}
/******长按结束******/
void longPressStop()
{Serial.println(LongPressStop); leds[0] CRGB(51, 51, 153); // 设置颜色为靛蓝FastLED.show();}
void attachPressStart()
{Serial.printf(attachPressStart,KEY STATE:%d\r\n,digitalRead(KEY));
}