Go Down

Topic: Will the Arduino crash if it never completes the main loop()? (Read 854 times) previous topic - next topic

I have had some stability issues with a couple of programs that I have written and I was wondering never finishing the main loop()
was causing the problem. Here is a simple program that demonstrates the problem.
Code: [Select]
void setup()
{
  pinMode(4,OUTPUT);
}

void loop()
{
  digitalWrite(4,HIGH);
  delay(100);
  digitalWrite(4,LOW);
  delay(100);
  loop();
   digitalWrite(4,HIGH);
  delay(1000);
  digitalWrite(4,LOW);
  delay(50);
 
 
}


This will blink an LED for around 5 minutes and then it stops blinking.
Does not ever making to the end of the main loop() cause problems?
If so, why does it cause problems?

Arrch

Code: [Select]

void loop()
{
 ...
 loop();
}


It's not crashing because it doesn't reach the end of the loop, it's crashing because you have recursion without an exit condition so what you're seeing is the result of stack overflow.

Docedison

Actually I think it is crashing because it can't reach the end of the loop, It keeps looping...

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

majenko

#3
Jun 30, 2012, 01:01 am Last Edit: Jun 30, 2012, 01:03 am by majenko Reason: 1
"To understand recursion you must first understand recursion."

Every time you call a function a whole load of stuff (including where you came from) is pushed on to the stack.  When your leave the function (return, or reach the end) all that data is removed from the stack.

If a function calls itself repeatedly with no way of "returning" from itself to the "itself" that called it, then the data on the stack is just going to grow and grow and grow.  Eventually the stack will smash in to the data for the variables in the running program - kind of like a car hitting a brick wall.  It crashes.

Arrch


Actually I think it is crashing because it can't reach the end of the loop, It keeps looping...

Doc


Code: [Select]

void loop() {

 while(1) {
   // Blink an LED
 }

 Serial.println("End of Loop");
}


Never reaches the end of the loop but never crashes.

majenko

What does it matter if it reaches the end of loop or not?

Code: [Select]

int main(void)
{
        init();

#if defined(USBCON)
        USBDevice.attach();
#endif

        setup();

        for (;;) {
                loop();
                if (serialEventRun) serialEventRun();
        }

        return 0;
}


As you can see from the contents of main.cpp, when the loop() function is run it just runs again.  (for(;;) is the same as while(1)).

wildbill

Quote
What does it matter if it reaches the end of loop or not?

Quite a lot if you're using the serial port  :P

Nick Gammon


Quite a lot if you're using the serial port  :P


If you are using serial events. Bit of a hack that. You can still use the serial port.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


This will blink an LED for around 5 minutes and then it stops blinking.


3.41 minutes I would guess.

Code: [Select]
void loop()
{
  digitalWrite(4,HIGH);
  delay(100);
  digitalWrite(4,LOW);
  delay(100);
  loop();  // -->> recurses back into loop, consuming 2 bytes of stack


2048 bytes of RAM, where you use 2 bytes every 0.2 seconds, would take 204.8 seconds to use all the RAM. That's 3.41 minutes.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Just checked with my stopwatch. 3 minutes 24 seconds (3.4 minutes). I love it when theory and reality agree!
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

James C4S


Just checked with my stopwatch. 3 minutes 24 seconds (3.4 minutes). I love it when theory and reality agree!

What are the two bytes?  The IP from where the jmp happened?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Coding Badly


Yup.  Return address (a stack filled with @loop).

Thanks for all of the reply's, they were very helpful and show me an area that I need to look into and understand better.

Go Up