【发布时间】:2016-11-06 14:38:04
【问题描述】:
我正在使用 arduino,我在 loop() 中创建了两种模式,在 while 循环中创建了 mode1,在 while 循环中创建了第二个模式,它们有条件要满足,我使用中断当前执行的例程的按钮在它们之间切换,更改标志以强制程序更改为第二种模式,并且由于某种原因而不是打破当前的 while 循环(因为不再满足条件)更改为另一个它只是不响应
或从自动更改->停止响应
#define modeSwitch 2
boolean manual;
boolean automatic;
String sOld;
boolean flagPrint = false;
volatile int modeSwitchValue;
String receivedData = "";
String invitation = "Welcome to Morse Code Encoder!\nPlease choose a mode: Write 'automatic' for an automatic mode or 'manual' or manual code.\n";
void setup() {
pinMode(modeSwitch, INPUT);
attachInterrupt(0, changeMode, RISING);
manual = false;
automatic = false;
volatile int modeSwitchValue = 5;
Serial.begin(9600);
Serial.println(invitation);
}
void loop() {
if (Serial.available() > 0) {
receivedData = Serial.readStringUntil('\n');
if (receivedData == "manual" ) {
manual = true;
automatic = false;
modeSwitchValue = 0;
}
if ( receivedData == "automatic") {
automatic = true;
manual = false;
modeSwitchValue = 1;
}
while ((manual == true) && (modeSwitchValue == 0) ) {
String s = "Manual mode:";
while (checkPrint(s) == true) {
Serial.println(s);
}
automatic = false;
}
while ((automatic == true) && (modeSwitchValue == 1)) {
String s = "Automatic mode:";
while (checkPrint(s) == true) {
Serial.println(s);
}
manual = false;
}
}
}
void changeMode() {
if ( modeSwitchValue == 0) {
modeSwitchValue = 1;
automatic = true;
manual = false;
}
if ( modeSwitchValue == 1) {
modeSwitchValue = 0;
manual = true;
automatic = false;
}
}
我删除了 checkPrint() 函数,因为它只确保消息被打印一次,所以我认为它没有必要在这里显示
我认为在执行自动代码时,我按下按钮并中断,更改模式变量然后回到程序中发生中断的同一位置。然后软件注意到自动模式的while循环条件不再满足,因此它跳到loop()并找到手动模式的while循环,因为条件满足。我想错了吗?还是我错过了什么?
【问题讨论】:
-
您希望
buttonState == LOW;和buttonState == HIGH;做什么? -
记住我们当前处于哪种模式以及接下来将更改为哪种模式
标签: c loops arduino interrupt-handling