|
本帖最后由 topdog 于 2021-3-27 21:45 编辑
网友的提问
对设问进行一下梳理和简化:
1,管脚4至13分别表示0到9。
2,管脚3的按键按下,检测到高电平停止计数,串口打印输出。
3,从100开始自加1,由LED显示数字。
譬如:123,第一、管脚13闪烁,第二、管脚12闪烁,第三、管脚11闪烁。
接线图:
略微解释一下:
1,整数类型数字可以直接赋值String实现转换,再用Arduino的字符串函数解析。
2,数字0至9对应的管脚数组,int PIN[] = {4, 13, 12, 11, 10, 9, 8, 7, 6, 5}; 步骤:解析出字符-->转成数字-->提取出数组中的对应管脚。
3,初始状态管脚通过按钮一端接在GND上,另一端接在高电平上,按下按钮后管脚检测到高电平。
[pre]String Number;
long number = 100;
const int pushButton = 3;
bool star = true;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT_PULLUP);
}
void loop() {
bool sensorVal = digitalRead(pushButton);
if (star) {
number += 1;
}
if (sensorVal == HIGH) {
star = false;
Serial.println(number);
} else {
star = true;
Number = number;
for (int i = 0; i < Number.length(); i++) {
char index = String(number).charAt(i);
blink(index);
}
}
delay(500);
}
void blink(char j) {
int PIN[] = {4, 13, 12, 11, 10, 9, 8, 7, 6, 5};
int k = HexChangeDec(j);
pinMode(PIN[k], OUTPUT);
digitalWrite(PIN[k], HIGH);
delay(200);
digitalWrite(PIN[k], LOW);
delay(200);
}
int HexChangeDec(char ch)
{
return (ch - '0' > 10) ? ch - '0' - 7 : ch - '0';
}[/pre]
引入onebutton库,按一次按钮停止计数打印现有数字,按两次按钮继续计数、解析数字并且在相应的位置闪烁。
[pre]#include "OneButton.h"
//typedef 将枚举类型定义成别名,利用该别名进行变量声明
typedef enum {
ACTION_STOP,
ACTION_RUN,
}
MyActions;
String Number;
bool star = true;
long number = 100;
const int delay_time = 100;
const int pushButton = 3;
//按钮高电平触发
OneButton button(pushButton, false, false);
MyActions nextAction = ACTION_RUN;
void setup()
{
Serial.begin(9600);
button.attachClick(singleClick);
button.attachDoubleClick(doubleClick);
//去除抖动的默认值为50,改为80毫秒
button.setDebounceTicks(80);
}
void loop()
{
button.tick();
if (star) {
number += 1;
}
if (nextAction == ACTION_RUN) {
star = true;
Number = number;
for (int i = 0; i < Number.length(); i++) {
char index = String(number).charAt(i);
blink(index);
}
} else if (nextAction == ACTION_STOP) {
star = false;
Serial.println(number);
delay(delay_time);
}
}
void singleClick()
{
nextAction = ACTION_STOP;
}
void doubleClick()
{
nextAction = ACTION_RUN;
}
void blink(char j) {
int PIN[] = {4, 13, 12, 11, 10, 9, 8, 7, 6, 5};
int k = HexChangeDec(j);
pinMode(PIN[k], OUTPUT);
digitalWrite(PIN[k], HIGH);
delay(delay_time);
digitalWrite(PIN[k], LOW);
delay(delay_time);
}
int HexChangeDec(char ch)
{
return (ch - '0' > 10) ? ch - '0' - 7 : ch - '0';
}[/pre]
可以参考《实现按钮的有限状态机(FSM)》,从一个按钮正确接线说起,再介绍了去除抖动的重要性,之后,推荐使用onebutton库在控制中用一个按钮实现有限状态机(finite state machine,简称:FSM)。最后介绍了实现onebutton库功能的简单流程和工作原理。https://www.arduino.cn/thread-103388-1-1.html
管脚2再加个提示LED,代码如下:
[pre]#include "OneButton.h"
typedef enum {
ACTION_STOP,
ACTION_RUN,
}
MyActions;
String Number;
bool star = true;
long number = 100;
const int reminder = 2;
const int pushButton = 3;
const int delay_time = 100;
//按钮高电平触发
OneButton button(pushButton, false, false);
MyActions nextAction = ACTION_RUN;
void setup()
{
Serial.begin(9600);
button.attachClick(singleClick);
button.attachDoubleClick(doubleClick);
//去除抖动的默认值为50,改为80毫秒
button.setDebounceTicks(80);
}
void loop()
{
button.tick();
if (star) {
number += 1;
}
if (nextAction == ACTION_RUN) {
star = true;
Number = number;
for (int i = 0; i < Number.length(); i++) {
char index = String(number).charAt(i);
blink(index);
}
reminder_blink();
} else if (nextAction == ACTION_STOP) {
star = false;
Serial.println(number);
delay(delay_time);
}
}
void singleClick()
{
nextAction = ACTION_STOP;
}
void doubleClick()
{
nextAction = ACTION_RUN;
}
void blink(char j) {
int PIN[] = {4, 13, 12, 11, 10, 9, 8, 7, 6, 5};
int k = HexChangeDec(j);
pinMode(PIN[k], OUTPUT);
digitalWrite(PIN[k], HIGH);
delay(delay_time);
digitalWrite(PIN[k], LOW);
delay(delay_time);
}
int HexChangeDec(char ch)
{
return (ch - '0' > 10) ? ch - '0' - 7 : ch - '0';
}
void reminder_blink(void){
pinMode(reminder, OUTPUT);
digitalWrite(reminder, HIGH);
delay(delay_time);
digitalWrite(reminder, LOW);
delay(delay_time);
}[/pre] |
|