arduino function to stop execution of code

is there a function in arduino that allows me to terminate the sketch completely? i want to use such a function inside my own custom function to terminate the sketch if a possible dangerous event occurs(to prevent the board from getting damaged).

void endItAll ()
{
  for {;;};
}

ryan27968:
is there a function in arduino that allows me to terminate the sketch completely? i want to use such a function inside my own custom function to terminate the sketch if a possible dangerous event occurs(to prevent the board from getting damaged).

You don't want to do this. Or you think you do then your project is not designed correctly.

void PowerDown()
{
  noInterrupts();
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
  sleep_cpu (); 
}

ryan27968:
to prevent the board from getting damaged

This part makes me think that your reason for wanting to do this is flawed - stopping the sketch does not give you any useful protection against damage as far as I can see.

(to prevent the board from getting damaged)

Old school, see attached schematic.
Power_DOWN is connected to an Arduino o/p pin. A Low will turn off the power.
Note: If you are switching higher than +5volt source drive K6 with a transistor.

7-3-2013 5-18-30 PM.jpg

Yes but if you have external voltages applied to the Arduino then the last thing you want to do is to turn off the power as that will damage the board.

Good point, the relay can be DPDT and turn off external supplies also.

ryan27968:
is there a function in arduino that allows me to terminate the sketch completely? i want to use such a function inside my own custom function to terminate the sketch if a possible dangerous event occurs(to prevent the board from getting damaged).

When your car detects that it is about to crash, it doesn't just "exit program". It does things like tighten the seatbelts, put on the brakes, deploy airbags, etc.

So what your sketch should do in a "dangerous event" is turn off things that might be making this event worse, not just bury its head in the sand.

wow. a lot of people are just judging instead of helping. i want to rewrite the analogRead and analogReference functions. as it says here: analogReference() - Arduino Reference it is dangerous to use analogReference after using the function analogRead. i want to rewrite it to terminate the analogReference function before it runs if the analogRead function has been previously run.

But that is nothing to do with stopping the code executing, right?

And you could have mentioned that in the first post. Might have helped clarify what you wanted.

wow. a lot of people are just judging instead of helping.

What you call judgment was help.
It turns out you do not want to do what you asked so the judgment was correct.
A reply can only be as good as the question.

sorry. so is there a way to do it?

ryan27968:
sorry. so is there a way to do it?

to do what?

  • i want to rewrite the analogRead and analogReference functions
    or
  • allows me to terminate the sketch completely
    ?

ryan27968:
wow. a lot of people are just judging instead of helping. i want to rewrite the analogRead and analogReference functions. as it says here: analogReference() - Arduino Reference it is dangerous to use analogReference after using the function analogRead. i want to rewrite it to terminate the analogReference function before it runs if the analogRead function has been previously run.

Sorry for my English, but I think the Warning at this url tell you another thing:
"Don't use anything less than 0V or more than 5V for external reference voltage on the AREF pin! If you're using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling analogRead(). "

A problem that predates programmable computers

i just need a way to terminate my sketch.

A sketch can't be terminated. In a MCU like Arduino the code is running in a loop()
The real program structure is (you can see in main.cpp under ...\arduino-IDE\hardware\arduino\cores\arduino folder) :

include <Arduino.h>
int main(void)
{  init();
#if defined(USBCON)
	USBDevice.attach();
#endif
	setup();
   	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}    
	return 0;
}

Your code in the loop() function is execute forever.
You can exit from the loop() like exit from a normal function, using return

void loop()
{ if(myError==true)
return;
... normal code that set to true myError when a critical error occour. myError is boolean global variable starting to false.
}

so i could just use the return function to only stop the dangerous analogReference function and allow the rest of the sketch to continue?