#include "OneButton.h"
#define BTN1_PIN 2
#define BTN2_PIN A0
#define ACTIVE_LOW true
#define ACTIVE_HIGH false
#define PULLUP_ENABLE true
#define PULLUP_DISABLE false
//按钮低有效,开启内部拉高
OneButton button1(BTN1_PIN, ACTIVE_LOW, PULLUP_ENABLE); //预处理写法
//OneButton button(BTN_PIN,true,true);
//第二个参数:true 低有效;false 高有效
// 第三个参数:true 开启内部拉高;false 关闭内部拉高
// 按钮高有效,关闭内部拉高
OneButton button2(BTN2_PIN, ACTIVE_HIGH, PULLUP_DISABLE);
//OneButton button(BTN_PIN,false,false);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
button1.setDebounceTicks(50);//按钮去抖动50毫秒
// 单击,双击,三连击,N连击
button1.setClickTicks(500);//时间判定500毫秒 半秒
button1.attachClick(singleClick1);//当button1单击时,运行singleClick1函数
button1.attachDoubleClick(doubleClick1);//当button1双击时,运行doubleClick1函数
button1.attachMultiClick(multiClick1);//当button1多击时,运行multiClick1函数
// 1000ms后,开启长按
button1.setPressTicks(800);//长按时间判定800毫秒后算长按
button1.attachLongPressStart(longPressStart1);//长按开始
button1.attachLongPressStop(longPressStop1);//长按结束
button1.attachDuringLongPress(longPress1);//长按
}
void loop() {
button1.tick();
button2.tick();
}
// 单击
void singleClick1() {
Serial.println("single click"); //click1
}
// 双击
void doubleClick1() {
Serial.println("Double Click"); // doubleclick1
}
//This function will be called once, when the button1 is press
void longPressStart1() {
Serial.println("Button 1 longPress start");
}// longPressStart1
// This function will be called often, while the button1 is prevoid
void longPress1() {
Serial.println("Button 1 longPress..."); // longPress1
}
// This function will be called once, when the button1 is relevoid
void longPressStop1() {
Serial.println("Button 1 longPress stop");
}// longPressStop1
void multiClick1(){
int n= button1.getNumberClicks();
switch(n){
case 3:
Serial.println("Triple click");
break;
case 4:
Serial.println("Quadruple click");
break;
default:
Serial.print("multiclick(");
Serial.print(n);
Serial.println(")detected.");
break;
};
}