arduino OneButton按键扫描,数码管显示,按键一长按,进入状态一:按键二按下,加一,按键三按下,减一 ,按键一短按,进入状态二:按键二按下,显示一个数,按键三按下,显示另一个数
时间: 2023-10-10 08:06:20 浏览: 156
按键扫描,数码管显示
好的,你需要一个arduino板和一个数码管。首先,你需要在arduino IDE中安装OneButton库,可以通过“工具”菜单中的“库管理器”来安装。
然后,你需要将数码管的引脚连接到arduino板上。假设你使用的是常见的共阳数码管,可以将其连接到数字引脚2-9上,其中每个引脚对应数码管的一位数字。你还需要连接一个按键到数字引脚10上,这是我们使用OneButton库的按键。
接下来,你需要编写一个程序来实现你的需求。下面是一个示例程序,可以作为你的参考:
```C++
#include <OneButton.h>
const int digitPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int buttonPin = 10;
const int longPressDuration = 1000; // 长按的持续时间,单位为毫秒
const int state2Number1 = 42;
const int state2Number2 = 123;
OneButton button(buttonPin, true); // true 表示使用内部上拉电阻
int currentState = 1;
int currentNumber = 0;
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(digitPins[i], OUTPUT);
}
button.attachClick(onButtonClick);
button.attachLongPressStart(onButtonLongPressStart);
}
void loop() {
if (currentState == 1) {
// 显示当前数字
displayNumber(currentNumber);
} else if (currentState == 2) {
// 显示固定的数字
displayNumber(state2Number1);
}
}
void displayNumber(int number) {
for (int i = 0; i < 8; i++) {
int digitValue = number % 10;
digitalWrite(digitPins[i], LOW); // 关闭所有数码管
digitalWrite(digitPins[i], HIGH); // 打开当前位的数码管
displayDigit(digitValue); // 显示当前位的数字
delay(5); // 稍微等待一下,避免闪烁
digitalWrite(digitPins[i], LOW); // 关闭当前位的数码管
number /= 10;
}
}
void displayDigit(int digit) {
switch (digit) {
case 0:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
break;
case 1:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
break;
case 2:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
break;
case 3:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(8, HIGH);
break;
case 4:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
case 5:
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
case 6:
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
case 7:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
break;
case 8:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
case 9:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
break;
}
}
void onButtonClick() {
if (currentState == 1) {
currentNumber++;
if (currentNumber > 99999999) {
currentNumber = 0;
}
} else if (currentState == 2) {
// 显示另一个数字
displayNumber(state2Number2);
}
}
void onButtonLongPressStart() {
currentState = 2;
currentNumber = 0;
}
```
这个程序使用了OneButton库来处理按键事件,可以识别短按和长按。在状态一下,按键一短按会将当前数字加一,按键一长按会进入状态二。在状态二下,按键一短按会回到状态一,按键二会显示一个数字,按键三会显示另一个数字。数码管的显示函数使用了一个简单的循环来遍历每一位数字,并且使用了一个显示数字的函数来实现每一位数字的显示。
阅读全文