我正在开发一个带按钮的Arduino项目。按下按钮后,我需要执行一些操作。问题是每当我在A6和A7引脚上使用按钮时,我会得到意想不到的结果,就像按钮接收到HIGH而不被物理按压。只要我使用带按钮的数字引脚,代码就可以正常工作。
请您解释一下使用A6和A7引脚可能出现的问题,或者我需要考虑使用这些引脚有些棘手的问题。
非常感谢!
这是我的代码:
int buttonBack = A6;
int flagLeft, flagRight;
int eventBack;
int currentButtonStatus = 0;
unsigned long currentButtonStatusStart1;
unsigned long currentButtonStatusStart2;
unsigned long currentButtonStatusStart3;
const int delayFalse = 30;
const int delayLongSingleClick = 300;
const int delayDeltaDoubleClick = 200;
void setup() {
pinMode(buttonBack,INPUT);
}
void loop() {
int eventBack = changeButtonStatus();
if (eventBack > 0) {
if(eventBack == 1) { // single button press
flagLeft = 1; flagRight = 0;
// move motor to the left code goes here
}
if(eventBack == 4) {
// long button press
flagLeft = 0; flagRight = 1;
// move motor to the right code goes here
}
}
}
int changeButtonStatus() {
int event = 0;
int currentButtonClick = analogRead(buttonBack);
unsigned long timeButton = millis();
switch(currentButtonStatus) {
case 0:
if(currentButtonClick) {
currentButtonStatus = 1;
currentButtonStatusStart1 = millis();
} else {
}
break;
case 1:
if(currentButtonClick) {
if(timeButton - currentButtonStatusStart1 >= delayLongSingleClick) {
event = 3;
}
} else {
if(timeButton - currentButtonStatusStart1 < delayFalse) {
currentButtonStatus = 0;
event = 0;
} else if(timeButton - currentButtonStatusStart1 < delayLongSingleClick) {
currentButtonStatus = 2;
currentButtonStatusStart2 = millis();
} else {
currentButtonStatus = 0;
event = 4;
}
}
break;
case 2:
if(currentButtonClick) {
if(timeButton - currentButtonStatusStart2 < delayFalse) {
currentButtonStatus = 1;
} else {
currentButtonStatus = 3;
currentButtonStatusStart3 = millis();
}
} else {
if(timeButton - currentButtonStatusStart2 > delayDeltaDoubleClick) {
currentButtonStatus = 0;
event = 1;
}
}
break;
case 3:
if(currentButtonClick) {
} else {
if(timeButton - currentButtonStatusStart3 < delayFalse) {
} else {
event = 2;
currentButtonStatus = 0;
}
}
break;
}
return event;
}
答案 0 :(得分:2)
A6和A7 具有GPIO功能。它们只能用作模拟输入,这意味着如果您尝试使用它们模拟数字输入,则需要提供外部上拉。