对于模糊的标题感到抱歉,但我不知道该放什么,因为我实际上并不知道我的问题是什么。我已经为连接到arduino的运动传感器编写了一些代码。每当我运行脚本时,它会绕过我出于某种原因标记的while循环,然后执行它下面的代码并停止。
int buzzPin = 8;
int upPin = 7;
int downPin = 6;
int motionPin = 12;
int redLed = 4;
int amberLed = 3;
int greenLed = 2;
void setup() {
pinMode(buzzPin, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(amberLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(upPin, INPUT);
pinMode(downPin, INPUT);
pinMode(motionPin, INPUT);
}
void loop() {
delay(1000);
digitalWrite(greenLed, HIGH);
digitalWrite(amberLed, LOW);
while(!digitalRead(upPin)==HIGH); //this is just bypassed when 'upPin' is not high
if(digitalRead(upPin)==HIGH){
digitalWrite(greenLed, LOW);
digitalWrite(amberLed, HIGH);
digitalWrite(buzzPin, HIGH); //delay before arming the motion sensor
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(250);
digitalWrite(buzzPin, LOW);
delay(250);
digitalWrite(buzzPin, HIGH);
delay(1000);
digitalWrite(buzzPin, LOW);
} //stops here...
while(!digitalRead(motionPin)==HIGH);
disarm();
alarm();
}
bool disarm(){ //button combination to disarm the motion sensor and stop the alarm
int i = 0;
while(true){
while(digitalRead(upPin)||digitalRead(downPin));
while(!digitalRead(upPin)||!digitalRead(downPin)){i++;
delay(1000);
if(i == 5){
return(false);
}
}
if(digitalRead(upPin)==HIGH){
int i = 0;
}
else{continue;}
while(digitalRead(upPin)||digitalRead(downPin));
while(!digitalRead(upPin)||!digitalRead(downPin)){i++;
delay(1000);
if(i == 5){
return(false);
}
}
if(digitalRead(upPin)==HIGH){
int i = 0;
}
else{continue;}
while(digitalRead(upPin)||digitalRead(downPin));
while(!digitalRead(upPin)||!digitalRead(downPin)){i++;
delay(1000);
if(i == 5){
return(false);
}
}
if(digitalRead(downPin)==HIGH){
int i = 0;
}
else{continue;}
while(digitalRead(upPin)||digitalRead(downPin));
while(!digitalRead(upPin)||!digitalRead(downPin)){i++;
delay(1000);
if(i == 5){
return(false);
}
}
if(digitalRead(upPin)==HIGH){
int i = 0;
}
else{continue;}
while(digitalRead(upPin)||digitalRead(downPin));
while(!digitalRead(upPin)||!digitalRead(downPin)){i++;
delay(1000);
if(i == 5){
return(false);
}
}
if(digitalRead(downPin)==HIGH){
int i = 0;
}
else{continue;}
while(digitalRead(upPin)||digitalRead(downPin));
while(!digitalRead(upPin)||!digitalRead(downPin)){i++;
delay(1000);
if(i == 5){
return(false);
}
}
if(digitalRead(downPin)==HIGH){
int i = 0;
}
else{continue;}
break;
}
return(true);
delay(50);
return(false);
}
void alarm(){
digitalWrite(redLed, HIGH);
digitalWrite(amberLed, LOW);
while(disarm()==false){
digitalWrite(buzzPin, HIGH);
delay(500);
digitalWrite(buzzPin, LOW);
delay(500);
}
}
如果有人能告诉我我的问题是什么以及如何解决它会很棒。
答案 0 :(得分:1)
如果引脚不高,你可以在第一个while循环的注释中写入它被绕过的注释。但是,在确定看起来很奇怪的情况之前,你有一个否定运算符(!)。我建议你删除它。之后的声明似乎也没有必要,因为它刚好在它检查的条件下进入循环。
答案 1 :(得分:1)
你的循环()
(这不是一个循环,而是一个非常持久的不间断延迟集合)
这样做:
void loop() {
show_green();
hang_until(upPin, LOW); // this can last forever
show_amber();
buzz_a_while(); // blocks a couple of seconds
hang_until(motionPin, LOW); // this seems to last forever
disarm();
alarm();
delay(1 sec);
}
所以简单地将upPin和motionPin强制为LOW,并且你不会注意到"停止"。
while()在arduino编码中始终是可疑的,因为它禁用了arduino的主要特性:永远运行循环函数。