Go Down

Topic: use of exit(0) (Read 23068 times) previous topic - next topic

n46130

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?

pYro_65

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.
Forum Mod anyone?
https://arduino.land/Moduino/

econjack

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.

michinyon

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.

Tom Carpenter

On Arduino, the statement:
Code: [Select]
exit(0);
Compiles to be:
Code: [Select]
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.
~Tom~

Grumpy_Mike

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

That is a very good idea.

Coding Badly


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

Tom Carpenter

Great minds think alike :).
~Tom~

Coding Badly


SirNickity

I love this forum.  <3

Coding Badly

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

Code: [Select]
#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();
  }
}


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

Tom Carpenter

#11
Feb 08, 2014, 01:09 pm Last Edit: Feb 08, 2014, 01:14 pm by Tom Carpenter Reason: 1
You could add the following for an Arduino Uno - this would reset all the pins to inputs with pullups disabled.
Code: [Select]
DDRC=0;
DDRB=0;
DDRD=0;
MCUCR |= _BV(PUD);
~Tom~

Coding Badly


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

retronet_RIMBA1ZO


...
...
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;
Code: [Select]

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

exit:
// shutdown/cleanup process

}

void loop() {}

Tom Carpenter

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?
~Tom~

Go Up