我有这个循环,我将如何结束循环?
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);
}
答案 0 :(得分:37)
这不是在Arduino.cc上发布的,但实际上您可以通过简单的退出(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.
}
我在将按钮连接到复位引脚的项目中使用它。基本上你的循环运行直到退出(0);然后只是坚持到最后一个状态。我为我的孩子制作了一些机器人,每按一下按钮(重置),代码就从loop()函数的开头开始。
答案 1 :(得分:8)
Arduino绝对没有办法退出他们的loop
函数,正如实际运行它的代码所展示的那样:
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
此外,在微控制器上,首先没有任何东西可以退出。
你最接近的就是停止处理器。这将停止处理,直到重置为止。
答案 2 :(得分:8)
Matti Virkkunen说得对,没有“体面”的方法来阻止循环。尽管如此,通过查看您的代码并做出一些假设,我想您正在尝试输出具有给定频率的信号,但您希望能够阻止它。
如果是这种情况,有几种解决方案:
如果您想通过输入按钮生成信号,可以执行以下操作
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 reference以获取有关此主题的更多信息,但简而言之,此配置设置了一个内部上拉电阻,这样您就可以将按钮接地,而无需外部电阻。
注意:这会反转按钮的级别,LOW
将被按下,HIGH
将被释放。
另一个选项是使用其中一个内置硬件定时器来获取一个周期性调用的函数。我不会深入了解here对它是什么以及如何使用它的一个很好的描述。
答案 3 :(得分:3)
想到的三个选项:
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
}
此选项运行您的代码一次,然后将Ard踢入 一个无尽的“隐形”循环。也许不是最好的方式 去,但就外表来说,它完成了工作 Ard将在旋转时继续吸取电流 一个无尽的圈子......或许可以建立一种计时器 这么多秒后让Ard进入睡眠的功能, 循环的分钟等...只是一个想法...肯定有 各种各样的睡眠图书馆......看 例如,Monk,Programming Arduino:Next Steps,pgs。,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踢入另一个无尽的“隐形”循环,但是这个
是时候反复检查if(stop_it == false)
中的stop_main_loop()
条件
在第一次通过后,每次都没有通过。
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.
}
当然还有其他方法可以“停止”令人讨厌的无休止的主循环 但这三个以及已经提到的那些应该让你开始。
答案 4 :(得分:2)
这将关闭中断并将CPU置于(永久直到复位/电源切换)睡眠状态:
cli();
sleep_enable();
sleep_cpu();
有关详细信息,另请参阅http://arduino.land/FAQ/content/7/47/en/how-to-stop-an-arduino-sketch.html。
答案 5 :(得分:0)
> #include <SPI.h>
> #include <MFRC522.h>
> #include <Ethernet.h>
>
> #define RST_PIN 4
> #define SS_PIN 2
>
> String content="";
>
> byte mac[]={0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; IPAddress
> ip(192,168,3,15);
>
> MFRC522 mfrc522(SS_PIN, RST_PIN); EthernetClient client;
>
> void setup() { Serial.begin(9600);
> SPI.begin(); while(!Serial){;} Ethernet.begin(mac, ip);
> mfrc522.PCD_Init();
> //Serial.println(F("Silahkan Scan Kartu RFID Anda:")); }
>
> void loop() { rfid(); database(); }
>
> void rfid(){ //membaca kartu RFID if ( !
> mfrc522.PICC_IsNewCardPresent()) {
> return; } // memilih salah satu card yang terdeteksi if ( ! mfrc522.PICC_ReadCardSerial()) {
> return; }
> //Serial.print("Kartu Anda Adalah :"); //String content= ""; //byte letter; //for (byte i = 0; i < mfrc522.uid.size; i++) {
> //Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
> //Serial.print(mfrc522.uid.uidByte[i], HEX);
> //content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
> //content.concat(String(mfrc522.uid.uidByte[i], HEX)); //} //Serial.println(); //delay(1000); //change value if you want to
> read cards faster
>
> //mfrc522.PICC_HaltA(); //mfrc522.PCD_StopCrypto1(); }
>
> void database(){ EthernetClient client; rfid(); if
> (client.connect("192.168.3.12", 80)){
> Serial.print("Kartu Anda Adalah :");
> String content="";
> byte letter;
> for (byte i = 0; i < mfrc522.uid.size; i++) {
> Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
> Serial.print(mfrc522.uid.uidByte[i], HEX);
> content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
> content.concat(String(mfrc522.uid.uidByte[i], HEX)); } Serial.println(); delay(1000); //change value if you want to read
> cards faster mfrc522.PICC_HaltA(); mfrc522.PCD_StopCrypto1();
>
> //Serial.println("connected");
> Serial.print(content);
> client.println("POST /gerbang2/insert.php HTTP/1.1");
> client.println("Host: 192,168,3,12");
> client.println("Connection: close");
> client.print("Content-Type: application/x-www-form-urlencoded\n");
> client.print("Content-Length: ");
> client.print(content.length());
> client.print("\n\n");
> client.print(content);
> Serial.println(content);
> delay (1000);
> client.stop(); } else{
> Serial.println("Connection Failed.");
> Serial.println();
> delay (1000); } }
如何在串口监视器中停止循环?
答案 6 :(得分:0)
只需使用此行退出功能:
return;