void setManual(){
//do something like turn on and off the light
}
void setAuto(){
for(;;){
digitalRead(pirPin); //read data from PIR
digitalWrite(ledPin, pirValue); // turn on and of the light follow the PIR's data
}
}我的问题是,当我调用setAuto()时,我不能转到另一个方法。
我对此一无所知。那么,PIR传感器可以在没有环路的情况下工作吗?或者,我如何才能打破这个循环,转到另一个方法?
发布于 2017-07-25 22:10:34
您不能使用另一种方法,因为
for(;;)是一个无限循环。
您可以在主loop()语句中使用计时器elapsed millis从传感器读取数据,中间可能会有一个延迟。有很多方法可以做到这一点。但是,当您的程序成熟到完成时,在单独的中输入无限循环可能不会做您想要的事情。Arduino代码中的主loop()已经是一个无限循环,也是您通常应该使用的循环。
发布于 2017-07-25 22:13:59
您已经使用for(;;)创建了一个无限循环。你可以试着这样做:
void setManual(){
//do something like turn on and off the light
}
void setAuto(){
bool flag=true;
int data;
while flag {
data = digitalRead(pirPin); //read data from PIR
if(data == 0) { //Specify a condition that can if triggered would change your flag to false and exit the loop
flag = false;
//break; //<-- You can also use this statement to break out of the loop.
}
digitalWrite(ledPin, pirValue); // turn on and of the light follow the PIR's data
}
}https://stackoverflow.com/questions/45305759
复制相似问题