Stop void loop () function

I’m working on a line follower robot , i have written the program but i’m interfacing a problem that i cant stop the “void loop ()” function !

I’ve read the reference and i know that “break” is used to stop the ordinary loop fns like while and for …

I need Ur help … thanks :slight_smile:

If you're done with it, just remove the battery.

for (;;);

GamalOthman: I'm working on a line follower robot , i have written the program but i'm interfacing a problem that i cant stop the "void loop ()" function !

Using what code?

How to use this forum

GamalOthman: i'm interfacing a problem that i cant stop the "void loop ()" function !

Don't put any code in it. Then it won't matter if it stops or not.

I’m working on a line follower robot …

Design your robot like this: Unplugger

Then it will stop, I guarantee it!

Keep a boolean variable named running or something similar, and default it to true. At the beginning of your loop() function, add something like if (!running) return;

Setting running to false will cause loop() to exit immediately.

Keep a boolean variable named running or something similar, and default it to true. At the beginning of your loop() function, add something like if (!running) return;

Setting running to false will cause loop() to exit immediately.

Exactly, this sometimes is called, flags, and are very useful when you wanna manage process, for example

boolean isGoal = false;

if(!isGoal)
{
  //Some code
}

else if(isGoal)
{
  //Another some code
}

I use to initialize my flags as false, but it depends of what you want.

if(isGoal)is not strictly needed because IsGoal can only be true or false

I recently uploaded a youtube video that talks through a few of the ways one might go about "stopping" void loop.

Entering "philomech How to Stop Void Loop " into the youtube search bar should take you to it.

Hope it's helpful.

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.

void loop()
{
  // whatever
}

void real_main_loop_you_dont_see()
{
  // do some stuff required behind the scenes
  while(1==1)
  {
    // do some stuff required between each loop - maybe timers or other kinds of things
    loop();
  }
}

What you can do, as others have already written is cause the loop to never end by going into an infinite loop, ie: doing your own "while(1==1);" Or you can have a variable (static or defined outside the loop) that is on/off / true/false that indicates whether or not to run any code within the loop.

Technically you can't "stop" the loop() function.

No, but you can put an infinite loop inside it, so that it never exits and therefore never gets called again. See my earlier post.

Why on earth you want to stop the loop() function?
I guess you need to sleep arduino rather than to completely stop the loop.

Arduino Sleep Example

Even then if you want loop() function to completely stop, just use following code and it would serve the purpose

if(<your halting condition>){
   while(1);
}

Let's start with WHY you want to stop the loop() function. Maybe there's a better way to approach it.

And what do you mean by stop? Do you want to break the execution of the current call, so that you can start the loop function over again from the top? Or do you really want to stop the program completely (which is what all of the suggestions so far are doing)?

I'm thinking this may be a classic case of wanting to do something, and thinking you have to do something specific, and are asking how to do that one thing. But in fact it's just part of a bigger task, and there might be a better solution rather than the one you are planning.

So, what are you actually trying to accomplish by "stopping" the loop() function?

GamalOthman: I've read the reference and i know that "break" is used to stop the ordinary loop fns like while and for ...

You call while and for "loop fns" but they aren't functions. They are statements that can repeatedly perform a block of code. Placing the "break" statement inside that block of code will terminate the loop, and transfer control to the next statement after the end of the loop.

But the loop() function is not a loop control statement like while and for: it is a function. The break statement has no meaning or effect on a function. If you want to completely break out of and exit a function, the simplest way is to just use the return statement. This will immediately exit the function, and return control to the function that called it. In this case, it will return to a hidden main function that will immediately loop back and call the loop() function again, from the start of the function.

Don't let the name of the loop() function confuse you. It's just a function, and doesn't inherently have anything to do with looping. The reason it loops is that another hidden function is calling it inside it's own loop. Pretend the setup() and loop() functions have different names, and what's going on might make more sense: - Instead of setup(), pretend the name is: functionThatGetsCalledOnceAtTheBeginning() - Instead of loop(), pretend the name is: functionThatGetsCalledOverAndOverAgainForever()

Technically you can't "stop" the loop() function.

What happens with:

void loop() {
   exit(0);
}

econjack: What happens with:

void loop() {
   exit(0);
}

IIRC, it disables interrupts and enters an infinite loop.

AWOL: IIRC, it disables interrupts and enters an infinite loop.

That is very likely. Normally, calling exit() terminates the running process and returns an exit code to either the operating system or parent process -- neither of which exist in a lightweight embedded system like Arduino. Terminating the process, or returning an exit code, doesn't make sense in this environment. Spinning in a death loop does.

ShapeShifter: That is very likely.

Not just likely, it is confirmed: AVR Libc Reference Manual - Function exit()

I got something right at last! Yay me!

I wanted to stop the loop() to save processor time. But since I figured I can't, I simply put nothing in it, left it at the end of the sketch and thereafter ignored it.

My program uses tone output to pin 2 and interrupt 0 to call a counter function. When the count exceeds some value it suspends interrupts, does some measurements, sets a control value according analog input, resets the count, increments the count, turns interrupts on again, and waits for the next interrupt. On the following fall of the tone signal on pin 2, the counter calls one or another function which uses the control value from above as a time in microseconds to switch an output on/off for PWM motor control, forward or reverse. The problem I had initially was to much time wasted processing commands instead of driving the motor and receiving an interrupt before the control time expired so limiting the effective PWM duty ratio. I couldn't use the analogWrite function as the frequency is too low and the motor windings scream loudly. I am running at 10kHz so interrupts are every 100us