Arduino Error Handler

What is the best way to create an Arduino Error Handler? I want it so that if I call the "error function" from anywhere it will display the error text on a lcd... Any ideas?

void setup() {
}

void loop() {
}

void error(char* err) {
  //print to the lcd
  //lcd.println(err);
  
  //do not return
  while(true) {}//halt execution, an error has ocurred
  //you could have the user do something if you want to progress from here
}

Maybe what you want, maybe not.

//do not return
// Might want to disable interrupts here
while(true) {}//halt execution, an error has ocurred
//you could have the user do something if you want to progress from here

:wink:

Im still a beginner with Arduino so can you explain how this works? What I want it to do is be able to call a function called "error" to display the error message on the lcd anywhere in the program.

You create a function, as Alpha Beta described. His function, with Coding Badly's addition, will halt the program in the error function. This may not be what you want to do, unless you include a method (like reading a button) to exit the error function.

Once you have written the function, you can call it from anywhere, at any time. Calling it from within the error function might not make sense, but you could do it.

Wait what is char* err?

The err variable is a pointer to a character variable (typically the first character in an array that is properly NULL terminated).

You can call that function in a variety of ways. The simplest is:

error("Oops. That wasn't what you wanted to do");

Or, you could have some pre-defined messages:

char msg1[] = "Wrong";
char msg2[] = "Stupid";

Then, pass one of those strings:

error(msg2);

Awesome! That is exactly what I wanted! Thanks again!! Ill ask you again if I have a problem...