我是Arduino编程的新手。我在编译以下代码时遇到问题:
const int relay1 = 10; //Power Relay 1
const int relay2 = 11; //Power Relay 2
const int relay3 = 12; //Toggle Relay
const int button1 = 3;
const int button2 = 4;
const int button3 = 5;
//---Button States---\\
int button1State; //Current state of Button 1
int button2State; //Current state of Button 2
int button3State; //Current state of Button 3
int button1State_prev = LOW; //Previous state of Button 1
int button2State_prev = LOW; //Previous state of Button 2
int button3State_prev = LOW; //Previous state of Button 3
//---General Variables---\\
int userSelection = 0;
int interlockState = 0;
int platformState = 0;
//---Interval-Tracking Variables---\\
unsigned long lastTime_Debounce1 = 0; //Button 1 debounce time
unsigned long lastTime_Debounce2 = 0; //Button 2 debounce time
//---Activity Delays---\\
const unsigned int relayDelay = 10; //Delay between relay actions (ms)
const unsigned int debounceDelay = 60; //Delay for button de-bouncing (ms)
void setup() {
//Configure Pins
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
void loop() {
//Read value of each input pin
int button1Reading = digitalRead(button1); //Current reading of Button 1
int button2Reading = digitalRead(button2); //Current reading of Button 2
int button3Reading = digitalRead(button3); //Current reading of Button 3
//Debounce Button1
if (button1Reading != button1State_prev) {
lastTime_Debounce1 = millis();
}
button1State_prev = button1Reading;
if ((millis() - lastTime_Debounce1) > debounceDelay) {
if (button1Reading != button1State) {
button1State = button1Reading;
}
}
//Debounce Button2
if (button2Reading != button2State_prev) {
lastTime_Debounce2 = millis();
}
button2State_prev = button2Reading;
if ((millis() - lastTime_Debounce2) > debounceDelay) {
if (button2Reading != button2State) {
button2State = button2Reading;
}
}
由于某种原因,编译器确信lastTime_Debounce1
第54行的第二个IF语句中的变量尚未在作用域内声明。我不知道这怎么可能,因为相关的变量是已定义和初始化的全局变量。
如果我注释掉IF语句的第一个三重奏(处理按钮1),则第二个三重奏(处理按钮2)编译没有问题,即使它以完全相同的方式执行完全相同的操作。
我检查了所有常见的可疑对象:一次拼写,花括号,分号,甚至注释掉了一段代码,但我找不到问题的根源。我正在使用Arduino 1.8.2 IDE。
有人可以指出我错过的错误吗?
1
我用C ++标记标记了您的问题,以查看语法高亮显示是否会使问题更加明显,但不幸的是没有。
—
尼克·加蒙