漠然としたタイトルの申し訳ありませんが、私は実際に私の問題が何であるか分からないので、何を置くべきか分からなかった。 Iv'eは、arduinoに接続されたモーションセンサーのコードを書いています。毎回スクリプトを実行するたびに、何らかの理由でマークされたwhileループをバイパスし、その下のコードを実行して停止します。私のarduinoに問題がある
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);
}
}
誰かが私の問題点を教えてもらえれば、それをどのように修正すればよいだろうか。
配線の写真を投稿する必要があると思います。 – Hampus
ちょうど推測ですが、while(digitalRead(upPin)!= HIGH);を試しましたか? – KMoussa