Pages: [1]   Go Down
Author Topic: Will the Arduino crash if it never completes the main loop()?  (Read 741 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3384
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2892
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Doc
Logged

--> 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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
« Last Edit: June 29, 2012, 06:03:24 pm by majenko » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3384
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Doc

Code:
void loop() {

  while(1) {
    // Blink an LED
  }

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

Never reaches the end of the loop but never crashes.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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(;smiley-wink is the same as while(1)).
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What does it matter if it reaches the end of loop or not?
Quite a lot if you're using the serial port  smiley-razz
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18819
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quite a lot if you're using the serial port  smiley-razz

If you are using serial events. Bit of a hack that. You can still use the serial port.
Logged


Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18819
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

3.41 minutes I would guess.

Code:
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.
Logged


Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18819
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12944
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: