I understand that every Arduino sketch has to contain a loop() function, which includes (directly or indirectly) essentially everything that happens other than the initialization stuff embodied in the setup() function. Thus, once it has started running, a sketch will run until the end of time or the Arduino board loses power, whichever happens first.
What happens if one wants the program to stop running if a particular set of conditions occurs? I supposes one could set a flag and then make everything in the loop() function subject to an "if (runFlag == true)" condition, and then set the flag to "false" if the things happen such that you want the program to stop. However, this seems awfully cumbersome (and it doesn't really stop the sketch from running, it just stops it from doing anything).
Triggering a Reset stops the sketch, but then immediately restarts it, so that doesn't do what I have in mind.
Is there a way to say "STOP! Don't do anything more, this program has FINISHED!" ?
I've been having this problem as well, I want to just stop one part (a function) of the sketch from running and then get another function to run afterwards based on values from the previous stopped function. If I place a boolean logic to stop it, the whole arduino sketch stops and nothing runs afterwards.
Placing things in the setup() just runs once and then the sketch does nothing.
Using a while(1) command seems to "freeze" the function in question and makes it useless for the duration of the sketch unless I restart the whole arduino but that defeats the purpose of what I'm trying to do.
madsci:
I've been having this problem as well, I want to just stop one part (a function) of the sketch from running and then get another function to run afterwards based on values from the previous stopped function.
You could declare a boolean variable inFirstState = true;.
Then, in loop() ...
if (inFirstState)
{
// your first part
if (/* time to change state */)
{
inFirstState = false;
}
else
{
// your second part
}
madsci:
I've been having this problem as well, I want to just stop one part (a function) of the sketch from running and then get another function to run afterwards based on values from the previous stopped function.
What Hackscribble wrote, plus look up the switch/case construct if you want more than two functions.
If you want something to run just once, put it is setup() and leave loop() empty. But the processor MUST be running some kind of program you can't "stop it".