C语言 如何停止循环arduino
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23096366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to stop a loop arduino
提问by Beep
I have this loop, how would I end the loop?
我有这个循环,我将如何结束循环?
void loop() {
// read the pushbutton input pin:
a ++;
Serial.println(a);
analogWrite(speakerOut, NULL);
if(a > 50 && a < 300){
analogWrite(speakerOut, 200);
}
if(a <= 49){
analogWrite(speakerOut, NULL);
}
if(a >= 300 && a <= 2499){
analogWrite(speakerOut, NULL);
}
采纳答案by Matti Virkkunen
Arduino specifically provides absolutely no way to exit their loopfunction, as exhibited by the code that actually runs it:
loop正如实际运行它的代码所展示的那样,Arduino 特别提供了绝对无法退出其功能的方法:
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
Besides, on a microcontroller there isn't anything to exit to in the first place.
此外,在微控制器上,首先没有什么可退出的。
The closest you can do is to just halt the processor. That will stop processing until it's reset.
您能做的最接近的事情就是停止处理器。这将停止处理,直到它被重置。
回答by Zoul007
This isn't published on Arduino.cc but you can in fact exit from the loop routine with a simple exit(0);
这未在 Arduino.cc 上发布,但实际上您可以使用简单的 exit(0) 退出循环例程;
This will compile on pretty much any board you have in your board list. I'm using IDE 1.0.6. I've tested it with Uno, Mega, Micro Pro and even the Adafruit Trinket
这几乎可以在您的电路板列表中的任何电路板上编译。我正在使用 IDE 1.0.6。我已经用 Uno、Mega、Micro Pro 甚至 Adafruit Trinket 对其进行了测试
void loop() {
// All of your code here
/* Note you should clean up any of your I/O here as on exit,
all 'ON'outputs remain HIGH */
// Exit the loop
exit(0); //The 0 is required to prevent compile error.
}
I use this in projects where I wire in a button to the reset pin. Basically your loop runs until exit(0); and then just persists in the last state. I've made some robots for my kids, and each time the press a button (reset) the code starts from the start of the loop() function.
我在将按钮连接到重置引脚的项目中使用它。基本上你的循环一直运行到 exit(0); 然后只是坚持最后一个状态。我为我的孩子制作了一些机器人,每次按下按钮(重置)时,代码都会从 loop() 函数的开头开始。
回答by Maximo Dominguez
Matti Virkkunensaid it right, there's no "decent" way of stopping the loop. Nonetheless, by looking at your code and making several assumptions, I imagine you're trying to output a signal with a given frequency, but you want to be able to stop it.
Matti Virkkunen说得对,没有“体面”的方式来停止循环。尽管如此,通过查看您的代码并做出几个假设,我想您正在尝试输出具有给定频率的信号,但您希望能够阻止它。
If that's the case, there are several solutions:
如果是这种情况,有几种解决方案:
If you want to generate the signal with the input of a button you could do the following
int speakerOut = A0; int buttonPin = 13; void setup() { pinMode(speakerOut, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); } int a = 0; void loop() { if(digitalRead(buttonPin) == LOW) { a ++; Serial.println(a); analogWrite(speakerOut, NULL); if(a > 50 && a < 300) { analogWrite(speakerOut, 200); } if(a <= 49) { analogWrite(speakerOut, NULL); } if(a >= 300 && a <= 2499) { analogWrite(speakerOut, NULL); } } }In this case we're using a button pin as an
INPUT_PULLUP. You can read the Arduino referencefor more information about this topic, but in a nutshell this configuration sets an internal pullup resistor, this way you can just have your button connected to ground, with no need of external resistors. Note: This will invert the levels of the button,LOWwill be pressed andHIGHwill be released.The other option would be using one of the built-ins hardware timers to get a function called periodically with interruptions. I won't go in depth be here's a great description of what it is and how to use it.
如果您想通过按钮输入生成信号,您可以执行以下操作
int speakerOut = A0; int buttonPin = 13; void setup() { pinMode(speakerOut, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); } int a = 0; void loop() { if(digitalRead(buttonPin) == LOW) { a ++; Serial.println(a); analogWrite(speakerOut, NULL); if(a > 50 && a < 300) { analogWrite(speakerOut, 200); } if(a <= 49) { analogWrite(speakerOut, NULL); } if(a >= 300 && a <= 2499) { analogWrite(speakerOut, NULL); } } }在这种情况下,我们使用按钮引脚作为
INPUT_PULLUP. 您可以阅读Arduino 参考以获取有关此主题的更多信息,但简而言之,此配置设置了一个内部上拉电阻,这样您只需将按钮接地,无需外部电阻。 注意:这将反转按钮的级别,LOW将被按下HIGH并将被释放。另一种选择是使用一个内置的硬件定时器来获取一个定期调用的函数,并有中断。我不会深入这里是对它是什么以及如何使用它的一个很好的描述。
回答by philomech
The three options that come to mind:
想到的三个选项:
1st) End void loop()with while(1)... or equally as good... while(true)
1st)void loop()以while(1)...结尾或同样好...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
}
This option runs your code once and then kicks the Ard into
an endless "invisible" loop. Perhaps not the nicest way to
go, but as far as outside appearances, it gets the job done.
The Ard will continue to draw current while it spins itself in
an endless circle... perhaps one could set up a sort of timer
function that puts the Ard to sleep after so many seconds,
minutes, etc., of looping... just a thought... there are certainly
various sleep libraries out there... see
e.g., Monk, Programming Arduino: Next Steps, pgs., 85-100
for further discussion of such.
此选项运行您的代码一次,然后将 Ard 踢入一个无休止的“隐形”循环。也许不是最好的方法,但就外表而言,它完成了工作。
Ard 会在它自己无限循环时继续汲取电流……也许可以设置一种计时器功能,让 Ard 在循环这么多秒、几分钟后进入睡眠状态……只是一个想法……那里肯定有各种睡眠库……请参见 Monk,Programming Arduino: Next Steps, pgs., 85-100 以进一步讨论此类问题。
2nd) Create a "stop main loop" function with a conditional control
structure that makes its initial test fail on a second pass.
This often requires declaring a global variable and having the
"stop main loop" function toggle the value of the variable
upon termination. E.g.,
2) 创建一个带有条件控制结构的“停止主循环”函数,使其初始测试在第二次通过时失败。
这通常需要声明一个全局变量并让“停止主循环”函数在终止时切换变量的值。例如,
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 ...
}
Granted, this might not be especially pretty, but it also works.
It kicks the Ard into another endless "invisible" loop, but this
time it's a case of repeatedly checking the if(stop_it == false)condition in stop_main_loop()which of course fails to pass every time after the first time through.
当然,这可能不是特别漂亮,但它也有效。
它将 Ard 踢进另一个无休止的“隐形”循环,但这一次是反复检查if(stop_it == false)条件的情况,stop_main_loop()在第一次通过后,当然每次都无法通过。
3rd) One could once again use a global variable but use a simple if (test == blah){}structure instead of a fancy "stop main loop" function.
3) 可以再次使用全局变量,但使用简单的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.
}
There are certainly other ways to "stop" that pesky endless main loop but these three as well as those already mentioned should get you started.
当然还有其他方法可以“停止”那个讨厌的无休止的主循环,但这三种以及已经提到的那些应该会让你开始。
回答by Sigivald
This will turn off interrupts and put the CPU into (permanent until reset/power toggled) sleep:
这将关闭中断并使 CPU 进入(永久直到复位/电源切换)睡眠:
cli();
sleep_enable();
sleep_cpu();
See also http://arduino.land/FAQ/content/7/47/en/how-to-stop-an-arduino-sketch.html, for more details.
另请参阅http://arduino.land/FAQ/content/7/47/en/how-to-stop-an-arduino-sketch.html,了解更多详情。

