Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop a loop arduino

Tags:

c

loops

arduino

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);
      }
like image 851
Beep Avatar asked Apr 15 '14 23:04

Beep


People also ask

Can you break out of Arduino loop?

The break command will exit a loop (including loop()). So if you want an interrupt to cause a loop to exit, then in your ISR, set a variable and check for that variable in your loop. If it's detected, then run the break command. It won't be real time, but it should be very fast.

Is there a stop function in Arduino?

Technically you can't "stop" the loop() function. There is another function that calls it repeatedly, and you can't modify that function without changing the way the compiler compiles the code and what it sends to the Arduino. ie.

How do you end an Arduino function?

No, exit is a system function that exits your app ( on arduino interrupts are disabled and an infinite loop locks the system ). Use the keyword break to stop a loop. To jump back to the top of a loop prematurely use the keyword continue.

How do I stop a program flashing in Arduino?

You could use one of the input pins to stop your program. You could check the level of the pin by interrupts or polling in the loop code. When you change the voltage of your input pin, your code will notice it and jump out the loop to stop the program. Other way is to control it with serial communication.


2 Answers

This isn't published on Arduino.cc but you can in fact exit from the loop routine with a simple 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

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.

like image 125
Zoul007 Avatar answered Oct 07 '22 15:10

Zoul007


Arduino specifically provides absolutely no way to exit their loop function, as exhibited by the code that actually runs it:

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.

like image 25
Matti Virkkunen Avatar answered Oct 07 '22 17:10

Matti Virkkunen