如何停止循环arduino

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

void loop() { // read the pushbutton input pin: a ++; Serial.println(a); analogWrite(speakerOut, NULL); if(a > 50 && a < 300){ analogWrite(speakerOut, 200); } if(a = 300 && a <= 2499){ analogWrite(speakerOut, NULL); } 

Arduino绝对没有办法退出它们的loop函数,正如实际运行它的代码所展示的那样:

 setup(); for (;;) { loop(); if (serialEventRun) serialEventRun(); } 

此外,在微控制器上,首先没有任何东西可以退出。

你最接近的就是停止处理器。 这将停止处理,直到它重置。

这不是在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()函数的开头开始。

Matti Virkkunen说得对,没有“体面”的方法来阻止循环。 尽管如此,通过查看您的代码并做出一些假设,我想您正在尝试输出具有给定频率的信号,但您希望能够阻止它。

如果是这种情况,有几种解决方案:

  1. 如果要通过输入按钮生成信号,可以执行以下操作

     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

  2. 另一种选择是使用其中一个内置硬件定时器来获取一个周期性调用的函数。 我不会深入到这里对这是什么以及如何使用它进行了很好的描述。

想到的三个选项:

1)结束void loop() with while(1) …或同样好…… while(true)

 void loop(){ //the code you want to run once here, //eg, If (blah == blah)...etc. while(1) //last line of main loop } 

此选项运行您的代码一次,然后将Ard踢入无限的“隐形”循环。 也许不是最好的方式,但就外表而言,它可以完成工作。
当Ard在无限循环中旋转时,Ard将继续消耗电流…也许可以设置一种计时器function,让Ard在经过如此多的秒,分钟等循环后进入睡眠状态……一个想法…那里肯定有各种各样的睡眠库……例如,参见Monk,Programming Arduino:Next Steps,pgs。,85-100进一步讨论。

2)使用条件控制结构创建“停止主循环”function,使其初始测试在第二次传递时失败。
这通常需要声明一个全局变量,并在终止时使用“停止主循环”function切换变量的值。 例如,

 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)条件的情况,当然在第一次通过后每次都无法通过。

3)人们可以再次使用全局变量,但使用简单的if (test == blah){}结构而不是花哨的“停止主循环”function。

 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, //eg, more If (blah == blah)...etc. } start = false; //toggle value of global "start" variable //Next time around, the if test is sure to fail. } 

当然还有其他方法可以“停止”令人讨厌的无尽主循环,但这三个以及已经提到的那些应该让你开始。

这将关闭中断并将CPU置于(永久直到复位/电源切换)睡眠状态:

 cli(); sleep_enable(); sleep_cpu(); 

有关详细信息,另请参阅http://arduino.land/FAQ/content/7/47/en/how-to-stop-an-arduino-sketch.html 。

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

如何在串口监视器中停止循环?