Hey guys,
well the title pretty much says what i am planning to do...
I was thinking about something like that:
void FUNCTION() {
some code here...
if(condition) {
"restart void loop"
}
some code there....
}
void setup() {
some things for the setup...
}
void loop() {
some code here....
FUNCTION();
some code there....
What do you think? Any ideas, how i could realize this?
some of you will say: "you could just write your function in the loop and use something like a break or a return..."
well, unfortunately this isn´t an option in my case
You should not think of using a function which is called from loop() to cause loop() to restart without allowing loop() to complete. If you could do that you would almost certainly cause your program to crash after a few iterations.
As @AWOL has said you can include code in loop() to make it terminate (return) if a variable is set by your function.
One option, if you really really had to do this, would be to find the place in the library code where loop() is called in the first place. You could modify that so when loop() returns you simply call loop() again. If you need it to be conditional then you might set up a conditional statement and variable to test before calling it again, or if there is a true program end then just enter an infinite loop that never ends and does not do anything rather than returning from loop() for the very last time.
There is most surely another way to do what you have to do though. For example, just encapsulate the code you have to repeat inside of loop() or in a function you call whenever you need to repeat it. That should work just as well and you dont have to modify anything outside of your own application code.
One option, if you really really had to do this, would be to find the place in the library code where loop() is called in the first place. You could modify that so when loop() returns you simply call loop() again.
6v6gt:
If you can't work within this structure, then you can override it with your own version of main().
Do not take this advice.
I cannot conceive of any reason that a microcontroller would need to have something other than the setup/loop idiom. If SomeBlackFire doesn't know how to make it work within that frame, I doubt there's any way they could make it work mucking around with the main function.
When I start thinking like this (quick clever tricks that should solve..), it's always a sign that I need to rethink the basic logic for solving the problem.
Also remember; if you recall loop(); Its going to carve out memory from your stack. And if you do it too much, without unwinding it, your memory is gone.
Rethink your program. It's always best to find a method that fits what the designer intended.
You don't restart the (ugh) "void loop". You write a loop that, as it runs normally, has the behaviour that you want. Rather than restarting when - for instance - a button is pressed, your write code that as part of it's operation watches the button and does whatever needs doing when it is pressed. If that involves resetting things to zero, you reset things to zero.
I guess most people are advising against this anyway now with the idea that there are better ideas that maintain the original basic structure of the Arduino code framework. I tend to believe that too because some libraries may come along that take that for granted.
In Windows programming we have one main() function too, but there doesnt seem to be any rule like loop() to follow so it's a bit more versatile. But there it would not be uncommon to write a new main() function for every new application while in Arduino Land it would be more uncommon. Would it really be a problem in Arduino though? I guess only time would tell
One problem i can think of offhand is that someone downloads the 'sketch' for that rewritten Arduino program and it does not run correctly and they cant figure out why. It turns out that they would also have to be sure to download the new 'main()' function as well or they would not get that significant piece of code that has the multiple loop() calls. It may take some time to figure out that they need that new main() function as well because that is not usually required in 'normal' sketchs.
MrAl:
But there it would not be uncommon to write a new main() function for every new application while in Arduino Land it would be more uncommon. Would it really be a problem in Arduino though? I guess only time would tell
How would it make any difference?
It would just change the OP's question to Restarting the "void main()" out of an other function ~ HOW?
AWOL:
Did you read reply #1?
It does what the OP requested - the only deviation from the spec (such as it was) is that FUNCTION returns a value.
No rewrite of main, no rewrite of loop.
Hi,
Sure, that's great. I guess people were looking for other solutions also.
Funny i never had to do any of this i just write inside loop mostly except for interrupts
MrAl:
Did you read the last paragraph of my previous post?
Also, it's not restarting main() it is rewriting main().
I know you were referring to "rewriting".
But I was trying to convey the idea that the OP does not have your depth of expertise and if the visible top level function was main() rather than loop() he would just transfer the same question to it.
Also his question (IMHO) does not need to be solved by rewriting main(). It needs to be solved by understanding how to use functions.
But I was trying to convey the idea that the OP does not have your depth of expertise and if the visible top level function was main() rather than loop() he would just transfer the same question to it.
Also his question (IMHO) does not need to be solved by rewriting main(). It needs to be solved by understanding how to use functions.
...R
Hi,
Yes i agree, and you can tell by the way i said the very same thing previously
I also stated that i never had to do that and i think that anyone that thinks they do need to do that just needs to look at a few more sketches or something to see that none of them do that, nor do they need to do that. I think everyone here made that very clear now too
SomeBlackFire:
Hey guys,
well the title pretty much says what i am planning to do...
I was thinking about something like that:
void FUNCTION() {
some code here...
if(condition) {
"restart void loop"
}
some code there....
}
void setup() {
some things for the setup...
}
void loop() {
some code here....
FUNCTION();
some code there....
What do you think? Any ideas, how i could realize this?
some of you will say: "you could just write your function in the loop and use something like a break or a return..."
well, unfortunately this isn´t an option in my case :/
The way your code ought to be structured is that loop() calls all the other functions, directly or
indirectly and they all return (in a timely manner). So you'd just return back into the loop.
Restarting loop is a red-herring, loop is always restarted constantly, its the event loop. It should be
running 1000's of times a second.