gpt4 book ai didi

c - 如何停止循环arduino

转载 作者:太空狗 更新时间:2023-10-29 16:42:01 24 4
gpt4 key购买 nike

我有这个循环,我该如何结束循环?

 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);
}

最佳答案

这没有在 Arduino.cc 上发布,但实际上您可以使用简单的 exit(0) 退出循环例程;

这几乎可以在您的电路板列表中的任何电路板上编译。我正在使用 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.
}

我在将按钮连接到重置引脚的项目中使用它。基本上你的循环一直运行到 exit(0);然后只是坚持最后的状态。我为我的 children 制作了一些机器人,每次按下按钮(重置)时,代码都会从 loop() 函数的开头开始。

关于c - 如何停止循环arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com