use of exit(0)

I am reading Jack Purdum's book "Beginning C for Arduino" and he used the statement exit(0). I cannot find this term in the Arduino control statement list. Is exit (0) a valid term to use to exit a loop?

No, exit is a system function that exits your app ( on arduino interrupts are disabled and an infinite loop locks the system ).

Use the keyword break to stop a loop. To jump back to the top of a loop prematurely use the keyword continue.

That guy's an idiot...oh, wait...that's me! The call to exit(0) is a standard way to end a program and it works on the Arduino, but it's not a good idea since there's really no op system to take the call, so to speak. The call was never intended to break out of some form of loop structure. On Unix and other op systems, it's a graceful way to terminate a program as it often cleans up resources and returns them to the op system. There's no real underlying op system for an Arduino.

2 Likes

What they said.

exit(0) will end the program, not a loop.

On an arduino, with no operating system and only one program, then "ending the program" doesn't really make sense.

On the arduino, calling exit(0) really isn't a useful thing to do. It should teach you to beware that, just because someone wrote a book about it, doesn't mean they are an expert.

In fact, if you want to "end the program" on an arduino, without turning it off or resetting it, you want to do the opposite, put it into an infinite loop that doesn't do anything.

On Arduino, the statement:

exit(0);

Compiles to be:

cli(); //disable interrupts
while(1); //forever loop

It basically stops your program running, but leaves the CPU running in an infinite loop. Personally I think it would be better if the exit() function put the chip into deep sleep mode.

2 Likes

Personally I think it would be better if the exit() function put the chip into deep sleep mode.

That is a very good idea.

1 Like

But not a new idea...
http://forum.arduino.cc/index.php?topic=58956.msg424464#msg424464
:wink:

Great minds think alike :).

  1. :slight_smile:

I love this forum. <3

1 Like

I love this forum. <3

No doubt!

I thought of two caveats with that code...

  1. To truly be "die" the watchdog needs to be disabled. This should do it...
#include <avr/sleep.h>
#include <avr/wdt.h>

void die( void )
{
  wdt_disable();
  set_sleep_mode( SLEEP_MODE_PWR_DOWN );

  while ( true )
  {
    sleep_enable();
    cli();
    sleep_cpu();
  }
}
  1. The output pins will retain their state. Any pins configured OUTPUT / LOW will continue sinking and any pins configured OUTPUT / HIGH will continue sourcing.

I believe everything else will be completely shutdown.

You could add the following for an Arduino Uno - this would reset all the pins to inputs with pullups disabled.

DDRC=0;
DDRB=0;
DDRD=0;
MCUCR |= _BV(PUD);

So, is there any interest in a "die" function or is this just an academic exercise?

michinyon:
...
...
In fact, if you want to "end the program" on an arduino, without turning it off or resetting it, you want to do the opposite, put it into an infinite loop that doesn't do anything.

would an alternative be something like;

void setup()
{
  while(1)
  {
    // main "loop" code
    goto exit; // instead of exit(0)
  }

exit:
// shutdown/cleanup process 

}

void loop() {}

not really.
For one millis() and Serial interrupts will still be firing.
Secondly SerialEvent() is still being called by loop.
Thirdly why on earth would you do it that way?

which it would still do in the example suggested by this person ?

put it into an infinite loop that doesn't do anything

to show my n00bness...

In our examples you will notice the cli() instruction which disables all interrupts. Also, we don't let loop() keep running (so nor will SerialEvent()).

Secondly SerialEvent() is still being called by loop.

Only if the function is defined, and only if there is serial data to process, and only if the end of loop() is actually reached. Calls to exit() make the last part the most unlikely event to actually happen..

PaulS:
Only if the function is defined, and only if there is serial data to process, and only if the end of loop() is actually reached. Calls to exit() make the last part the most unlikely event to actually happen..

You don't know that its not defined. The whole point in a function that is supposed to stop everything is that it stops everything and doesn't leave open the possibility that the user has left executable code that can still be run.
You'll also notice that in that example exit() is not called, but rather he has used a goto; statement to break out of a loop.