在 If 命令中重复 If 命令

问题描述

假设我有一个传感器可以读取运动。如果它检测到运动,那么它会做一些事情。完成上述操作后,传感器再次检查运动。如果它没有检测到任何移动,则它会继续执行下一个命令。如果它仍然检测到移动,则它会继续检查移动,直到不再检测到任何移动。我如何重复一个 if 命令,一个 if 命令中

if(movement = yes){
 statement;
 delay;
 }if(movement still equals yes){
  repeat and check again until you get the other result
 }if(movement = no){
  statement;

解决方法

看看while()循环here,让它成为loop()函数的一部分。

,

Arduino 已经有一个循环 void loop (),所以在循环内你必须只包含一次 if 语句,它会继续工作。

bool detection;
void loop () {
  if ( detection == true ) 
  {
    statement;
  }else
  {
    the othe statement;
  }
}

我希望这就是你要找的。​​p>