Writing and calling functions

Hi folks,

Another programming question for the experts:

I am making a system and want to define error conditions.
Every component on the system will be able to clear a bit in case of an error condition, which i would like to make a function an call. I am just a bit unsure on how to use functions.

For example say I wanted to call a function named void error() or something.
Any examples I could use?

JimboZA:
http://arduino.cc/en/Reference/FunctionDeclaration

Cool, thats exactly what i was looking for.

In fact I still have a question:

Say on this error function I want to send a error message.

The code is stopped and an error message is printed to an LCD. How shall i write it so that the error condition is sent as well?

How shall i write it so that the error condition is sent as well?

Three servos. The first moves the second one. The second one moves the third one. The third one has a pen attached. Read the appropriate move instructions from an SD card, and make the servos move. The pen will then write out the message. All you'll need to do is put it in an envelope, address it, put a stamp on it, and take it to the post office.

Or perhaps you had something completely different in mind in terms of "sending an error condition".

We've hired a mind reader, but his start date is the 12th of never. If you need help before then, don't rely on our (poor) abilities to read minds.

Paul, did you write all of that or it is part of your templates XD

I want an error LED to light up, attached to digital pin 7, and a message to be sent either via serial or to a character LCD.

I dont know how to transfer the "written" message that will appear on serial/LC to the function.

I dont know how to transfer the "written" message that will appear on serial/LC to the function.

char *errorMessage = "Hey, who let the magic smoke out?";

void ShowError(char *pMess)
{
   Serial.print(pMess);
}

void setup()
{
   Serial.begin(115200);
   ShowError(errorMessage);
}

void loop()
{
}

Coming soon.

2014-08-13_10-52-32.jpg

Slight modification to Paul's code:

#define ERRORLED 7

char *errorMessage = "Hey, who let the magic smoke out?";
boolean errorFlag = false;

void ShowError(char *pMess)
{
   Serial.print(pMess);
}

void ShowErrorLED() {
   digitalWrite(ERRORLED, HIGH);
}

void setup()
{
   Serial.begin(115200);
   ShowError(errorMessage);
}

void loop()
{
   // Some code that could cause an error and set errorFlag
   if (errorFlag == true) {
      ShowError(errorMessage);   // On LCD
      ShowErrorLED();            // On LED
      errorFlag = false;
   }
   // more code...
}

You would still need to add a function that would clear the error flag and turn off the LED. It's important to use two different functions for the error message and the error indicator so if you recode one you don't need to recode the other. Good rule of thumb: One function, one task. Don't write functions that try to be a Swiss army knife.

Im a bit confused, excuse my ignorance on the subject
While this amazing mind reader doesn't get here let me try to explain what i need and feel free to ask if needed:

So I have a given condition in my loop

if( x>0)
     {
     error =1;
     }

Typically the error is set to one but the loop continues going untill the end, so I could write:

if( x>0)
     {
     error =1;
     call function error;
     error code description
     }

Now inside my function error I want to do a few things:

void function 12345()
{disable output x
enable error led
print error message to lcd/serial
check input z to see if the user has asked the error condition to be cleaned

if (the error condition is still true)
     {
     Go back to the beginning of this function and check everything again;
     }
else
     {
     exit function and go back into the main loop again
     }

}

The main issue is that i don't want the loop to be executed until this error is cleared so i want the function to keep looping within itself.

The main issue is that i don’t want the loop to be executed until this error is cleared so i want the function to keep looping within itself.

Then, use a while loop. Break out when the condition has been corrected.

It’s time to get more specific about what you are trying to do. What errors do you think you can detect that the user can correct without powering off the Arduino?

PaulS:

The main issue is that i don’t want the loop to be executed until this error is cleared so i want the function to keep looping within itself.

Then, use a while loop. Break out when the condition has been corrected.

It’s time to get more specific about what you are trying to do. What errors do you think you can detect that the user can correct without powering off the Arduino?

I have an ADC reading, so 0 to 1023, representing a voltage

Values 0 to 500 and 900 to 1023 are error conditions and indicate a fault.

On such instances I set an error variable to 1 and want to avoid executing the loop.

The user can either power the arduino off or press a button to clear the fault.

Something like this:

if ((adc <500) | (adc >900))
error = 1;

Now, how can I “stop” the code until error is set to zero?

want to break the code until

Values 0 to 500 and 900 to 1023 are error conditions and indicate a fault.

Why? Undervoltage and overvoltage?

On such instances I set an error variable to 1 and want to avoid executing the loop.

It's best to be clear what you mean. If it is loop() you want to avoid executing, say that.

The user can either power the arduino off or press a button to clear the fault.

Nonsense. Pressing a switch can NOT change the voltage being read. The user could power off the Arduino and change the voltage source. The user might be able to turn a potentiometer to change the voltage.

Again, you need to be much more specific. It looks to me like you are looking for a software-only solution to hardware issues.

PaulS:

Values 0 to 500 and 900 to 1023 are error conditions and indicate a fault.

Why? Undervoltage and overvoltage?

Both

PaulS:

On such instances I set an error variable to 1 and want to avoid executing the loop.

It's best to be clear what you mean. If it is loop() you want to avoid executing, say that.

Yes, i don't want to execute anything inside the main loop

PaulS:

The user can either power the arduino off or press a button to clear the fault.

Nonsense. Pressing a switch can NOT change the voltage being read. The user could power off the Arduino and change the voltage source. The user might be able to turn a potentiometer to change the voltage.

It will toggle logic states if the pin is set as a digital input.

If cant disable execution of the whole main loop.
What you can do is this:

void main()

if error condition ==0{
   {
   Your main loop code goes here
   }
else if error condition ==1
   {
   check your digital input 
      if (button ==0){
           error ==0;
            }
   //then check the condition again
   if error condition ==1{
           print error message;
           turn fire extinguisher on!;
           }
}

Brilliant idea!

The only issue i see is that the loop keeps being executed until the end until the error occurs. is there a function I could use to go back to the the beginning of the loop?

PaulS:

The main issue is that i don’t want the loop to be executed until this error is cleared so i want the function to keep looping within itself.

Then, use a while loop. Break out when the condition has been corrected.

//He means like this.

Int error;
Int analogPin;

void setup(){
analogPin=analogRead(A0);
}
void loop(){
while (analogPin< 500 && analogPin>900){
delay(20);
}

This will trap the main looping code in the while “trap” untell the value is corrected.
I think

while (analogPin< 500 && analogPin>900){

Yes, but I doubt the pin will be both < 500 and > 900 at the same time.

Plus you are not taking a reading inside that loop.

Maybe:

while (analogRead (A0) < 500 || analogRead (A0) > 900)
{
delay(20);
}

yes thank you. You are correct