想到的三个选项:
1st) 以 while(1)... 结束 void loop()... 或同样好... while(true)
void loop(){
//the code you want to run once here,
//e.g., If (blah == blah)...etc.
while(1) //last line of main loop
}
此选项运行您的代码一次,然后将 Ard 踢入
一个无休止的“隐形”循环。也许不是最好的方式
去吧,但就外表而言,它完成了工作。
Ard 将在其自旋时继续汲取电流
一个无尽的循环......也许可以设置一种计时器
让 Ard 在几秒钟后进入睡眠状态的功能,
分钟等,循环......只是一个想法......当然有
那里有各种睡眠库......见
例如,Monk,Arduino 编程:下一步,第 85-100 页
进一步讨论。
2nd) 创建一个带有条件控制的“停止主循环”函数
使其初始测试在第二次通过时失败的结构。
这通常需要声明一个全局变量并具有
“停止主循环”功能切换变量的值
终止时。例如,
boolean stop_it = false; //global variable
void setup(){
Serial.begin(9600);
//blah...
}
boolean stop_main_loop(){ //fancy stop main loop function
if(stop_it == false){ //which it will be the first time through
Serial.println("This should print once.");
//then do some more blah....you can locate all the
// code you want to run once here....eventually end by
//toggling the "stop_it" variable ...
}
stop_it = true; //...like this
return stop_it; //then send this newly updated "stop_it" value
// outside the function
}
void loop{
stop_it = stop_main_loop(); //and finally catch that updated
//value and store it in the global stop_it
//variable, effectively
//halting the loop ...
}
当然,这可能不是特别漂亮,但它也有效。
它将 Ard 踢进了另一个无休止的“隐形”循环,但这
这是在stop_main_loop() 中反复检查if(stop_it == false) 条件的情况
在第一次通过之后,当然每次都无法通过。
3rd) 可以再次使用全局变量,但使用简单的if (test == blah){} 结构而不是花哨的“停止主循环”函数。
boolean start = true; //global variable
void setup(){
Serial.begin(9600);
}
void loop(){
if(start == true){ //which it will be the first time through
Serial.println("This should print once.");
//the code you want to run once here,
//e.g., more If (blah == blah)...etc.
}
start = false; //toggle value of global "start" variable
//Next time around, the if test is sure to fail.
}
当然还有其他方法可以“停止”讨厌的无休止的主循环
但是这三个以及已经提到的应该可以帮助您入门。