Beep Beep - 3 months ago 40
C Question

how to stop a loop 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);
}

Answer

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.

Comments