calling void loop as a function inside program?

Hi my question is this: Can i call void loop() inside the sketch (in a function outside the void loop), to restart the void loop()?

Ex:

int mainMenu()
{
if (x == 69)
loop();
}

I tried this and it seems to restart the whole arduino from before the void loop(), it seems to run through the void setup() first for some reason.

Calling loop from elsewhere in the program is dangerous. It leaves the old call that was running that called the function that called loop sitting on the stack. So it's sort of like a function calling itself. That's called recursion and if you don't know what you're doing with recursion you end up with the stack and the heap running into each other and the program crashing.

Post the rest of your code. There is very likely a better way to solve your problem.

OK; ill avoid doing this. i'm basically trying to simplify coding, I have over 10000 lines of code and a complex menu system. I was hoping to do this instead of having a pile of goto's in the program or something else that will take too much effort to implement.

Is it possible to reset the arduino inside a function? the only problem i have with this is at least one digitalout would have to stay on as it hold power to keep my project from powering down.

instrumentek:
Can i call void loop() inside the sketch (in a function outside the void loop), to restart the void loop()?

Yes, you can!
But it’s totally worthless for what you want.

instrumentek:
I tried this and it seems to restart the whole arduino from before the void loop(), it seems to run through the void setup() first for some reason.

What you do is “recursion”. While loop() is running, you call another time loop().
Recursion will always put the return address on the stack when the function is called, which eats up available RAM.

If the same function is called over and over without exit condition, the recursion will never end, and after a limited number of function calls it means “stack overflow” and perhaps your sketch will restart then. That’s what you observe: Restarting the sketch due to stack overflow after unlimited recursion.

That’s as useful as a cat biting itself in its own tail while chasing around.

Delta_G:
Post the rest of your code. There is very likely a better way to solve your problem.

jurs:
Yes, you can!
But it's totally worthless for what you want.

What you do is "recursion". While loop() is running, you call another time loop().
Recursion will always put the return address on the stack when the function is called, which eats up available RAM.

If the same function is called over and over without exit condition, the recursion will never end, and after a limited number of function calls it means "stack overflow" and perhaps your sketch will restart then. That's what you observe: Restarting the sketch due to stack overflow after unlimited recursion.

That's as useful as a cat biting itself in its own tail while chasing around.

OK that makes scene, ram usage is maxed out so i'm going to try and come up with something else. maybe the dreaded "goto" and many of them hahahah

Delta_G:

well i cant do it now, code is 10K+ lines i could go home(at work now) later, edit it and post an example of what i want to do.

basically i have a keypad(running on interrupts) that controls the program because of timing and such each portion of the program needs special placement of commands driven by the keypad. i want one keypad button to return the program to start. now i did implement a function that is executed on every while loop with consideration for my keypad timing issue.

My idea was the use something inside that function to send the program back to void loop(). or i will need a pile of goto's inside the program or a whole pile of reprogramming.

instrumentek:
OK that makes scene, ram usage is maxed out so i'm going to try and come up with something else. maybe the dreaded "goto" and many of them hahahah

That's an even worse idea...

Delta_G:
Post the rest of your code. There is very likely a better way to solve your problem.

If it's too long to post, just put it as an attachment to your post.

The Title of your Post has the logic of the program upside-down.

Other functions should be called from loop(). Not loop() from other functions.

Re-organize the program.

You might get some ideas from planning and implementing a program

...R

If you think you need a lot of goto's, or to call loop() from somewhere within your program, there is a serious problem with the design of your application.

Regards,
Ray L.

There is a neat way to structure code when you have some high priority fast tasks and
some slower (but less urgent) tasks.

Code the tests for urgent tasks in a function called something like “urgent_loop()”,
and the tests for less urgent ones in loop().

Add a call to urgent_loop() from loop () too.

Then you can have your less urgent (but perhaps time consuming) code call
urgent_loop() in various places to ensure the urgent stuff is handled in a timely
manner. Since only loop() calls your less urgent code, this avoids the recursive
call back to loop(), but still allows the urgent stuff to be handled in a simple
tidy way.

An example of “less urgent stuff” might be parsing some incoming serial string,
where you might want to read in a packet of data thus:

void handle_serial_packet ()
{
  for (i = 0 ; i < PACKET_SIZE ; i++)
  {
    while (! Serial.available ())
      urgent_loop () ;            // ensure urgent stuff always checked frequently
    packet [i] = Serial.read () ;
  }
  process_packet () ;
}

void loop ()
{
  if (Serial.available ())
    handle_serial_packet () ;
  if (....)
    handle .....
  urgent_loop () ;
}

void urgent_loop ()
{
  if (.....)
    ...... ;
}

This idea can be extended, within reason, to several levels of urgency if wanted,
and can improve the readability of some of the code by avoiding too many
state machines.

instrumentek:
OK; ill avoid doing this. i'm basically trying to simplify coding, I have over 10000 lines of code and a complex menu system. I was hoping to do this instead of having a pile of goto's in the program or something else that will take too much effort to implement.

Ok. Data structures. If you have menus and submenus and so on, then to do the job properly you will need a data structure, and C++ classes here are probably the way to go - although you can use structs and use pointers to functions.

You have an object of type menu item. The default type of menu item is a "menu" - it holds an array of other menu items in it and allows the user to select from them. You have specific menu item types for your actions.

The loop holds as part of its state the "current" menu item that the user is looking at. The menu objects take input and update the state. The leaf actions do whatever they do and probably also update the state - moving the user back to the root item or whatever.

The code is (admittedly) complex, but modular. I'll see if I can code something up and put it in the gallery.

Dude, I have built a menuing system in a sample sketch at

Hopefully you might be able to adapt it to your needs.