[SOLVED]How to stop a running user defined function inside void loop() ?

Hi everyone :slight_smile: ,

I wrote a function and called it inside the void loop() to run for 30mins. But i want to put a button to stop the function in case i want to stop it before 30 mins. One way of doing this is RESET button. But it takes time to reset and not an option for me.

I even put the code for the stop button inside the function.But then it makes the process stop for some time and kicks back in again.

At this point pretty confused where to put this piece of code??

Make running the function dependant on a boolean variable. Run it only when the boolean is true. When the button becomes pressed set the boolean to false.

By the way, spending 30 minutes in a function sounds like it is not a good idea,

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

UKHeliBob:
By the way, spending 30 minutes in a function sounds like it is not a good idea,

Why do you think that running 30 mins is not good idea?

And your idea worked completely fine.... Thanks for the help.

Robin2:
Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

Mine was a little simpler... But this is a new thing i learned. Surely i will use it later.
Thanks for the reply.

mainakb007:
idea worked completely fine

One thing I always consider when leaving a function is that there may be some tidying up to do; this may or may not be important to the OP right now but it's worth mentioning

Say that function was blinking an led, ala blinkwithoutdelay. You may have a requirement that the led must be off if it's not actually blinking. It's very possible (50% possible if the on- and off-times are equal) that when you read the button in loop() and set the boolean (let's call it weAreBlinking) to false, you would be doing that in an on-phase. That would leave the led on.

So to tidy up, when you are in the state of !weAreBlinking, you would (in my hypothetical example) need to explicitly set the ledState as low.

Why do you think that running 30 mins is not good idea?

Running a function for 30 mins is fine but it means that any code in loop(), apart from the function call, is not run for 30 mins. That might not matter for your current project but it might for others.

For instance, it would be neater to read the button input in loop() and have your function return to loop() quickly after it executes a small part of whatever it is doing. That way you could have another function, written in the same way that does something completely different and call that from loop() too. It also makes it easier to manage user input such as short press of button 1 stops the function being called, long press restarts the function from where you left off, longer press restarts the function from the beginning etc

The reading of the button input could conveniently be put in a function of its own with a sensible name. This makes inputs easier to find and test and gives you the opportunity of testing the input reading without the need to even call the other function(s) used by the program or for them to even be present in the test code.

I would be interested to know what the function does for 30 mins and whether it uses delay() for timing. Would you care to share it with us ?

not_a_noob:
One thing I always consider when leaving a function is that there may be some tidying up to do;

[....]

So to tidy up, when you are in the state of !weAreBlinking, you would (in my hypothetical example) need to explicitly set the ledState as low.

I deal with that by always calling the function (whether it will be needed or not) and, as the first thing within the function check the variable that determines whether the function is required. As part of that step the function could ensure (for example) that the LED is off.

This style also keeps the code in loop() much cleaner.

...R