Error function gives void error problem - how to call?

Moving on from my debug variable problem (discussed elsewhere), I'm trying to get the next steps in my code working.

I have a couple of LED's that I want to control as a small part of my project.

First is a green LED that turns on when I'm in the loop function, and ready to accept commands, but is turned off while setting up, or in case of an error. I mostly have this working, except for the error handling.

Second is a red Error Condition LED that I want to turn on, and eventually flash an error code, if there is a problem. I can turn this on and off in the main part of my code, so I know the wiring is correct.

Since I may encounter an error in several places it seems to me like it would be best to have an Error function with code to switch the LED's, and also (eventually) return the mechanical bits to a safe state...

I'm thinking that the function should ideally NOT return if that is possible (is it?) but just get stuck in the error handler until the board is reset (presumably after fixing the problem...)

In order to have an error code flashed, I presumably need to make the function take an error code variable, which I think should be an int.

This is my test code so far, with the non-relevant bits chopped out:

    // DEBUG SWITCH VARIABLE
    const int debugPin = 6;
    int debug = 1;

    // LED indicator variables
    const int runledPin =  7;    // the number of the RUN LED pin
    int runledState = LOW;       // Run LED state

    const int errorledPin =  8;  // the number of the ERROR LED pin
    int errorledState = LOW;     // Error LED state

    int error = 0;             // Error has happened (if high)
    int errorCode = 0;           // what is wrong
    
    
void setup() {
        // DEBUG SWITCH SETUP - NOTE switch ON = 0
        pinMode(debugPin, INPUT_PULLUP);
        debug = digitalRead(debugPin);

        // initialize serial communication at 9600 bits per second (only needed
        // for debugging):
        if (debug == 0) {
            Serial.begin(9600);
        }
        // LED pin setups        
        pinMode (runledPin, OUTPUT);   // RUN LED
        pinMode (errorledPin, OUTPUT); // ERROR LED
    } // SETUP END

void loop() {
        // Turn on RUN LED
        runledState = HIGH;
        digitalWrite(runledPin, runledState);

        
        if (0 == debug) {
            delay(1000);
        }

//  Basic function test - LED turns on when expected
//        errorledState = HIGH;
//        digitalWrite(errorledPin, errorledState);



        // errorFunction test
        errorCode = 1;
       error = errorFunction;
        
    } // END LOOP function

void errorFunction (int errorCode) {

        // Out of loop, turn off run LED
        runledState = LOW;
        digitalWrite(runledPin, runledState);
  
        // Something is wrong, turn on error LED
        errorledState = HIGH;
        digitalWrite(errorledPin, errorledState);
        
    
    } // END errorFunction

This gives me the following when I try to compile it - error_function_test:0: error: variable or field ‘errorFunction’ declared void error_function_test:0: error: ‘errorCode’ was not declared in this scope error_function_test.cpp: In function ‘void loop()’: error_function_test:46: error: ‘errorFunction’ was not declared in this scope error_function_test.cpp: At global scope: error_function_test:50: error: variable or field ‘errorFunction’ declared void

Various other combinations of putting errorCode into the function calls, with and without the "int" give me other errors that are similar.

I've done searches on the error messages, and looked at the playground docs on function writing, and can't see what I am doing wrong.

The function doesn't return, and wouldn't return a value if it did, so I think it should start with (void), but it does need to pull in the errorCode variable (even if it doesn't do anything with it right now) so I need to put some kind of stuff in the parentheses after the function name, but what?

Thanks, ex-Gooserider

There are multiple problems with this line:

error = errorFunction;

What are you trying to do here exactly? To call a function you need brackets, eg.

error = errorFunction (errorCode);

And since it is void it doesn't return a result, so don't try to assign it, eg.

errorFunction (errorCode);

The function doesn't return ...

All functions return, but you can delay them:

void errorFunction (int errorCode) {

...

while (true) 
  {}

} // END errorFunction

Now it returns when the while loop finishes. Which is never.

Thanks Nick, but I’m still not having much luck…

What I’m trying to accomplish eventually is to create an error handling function, so that if I detect an error anywhere in the code, or possibly a mechanical glitch, I will call the function with an error code that will give some diagnostic data. The function should then loop until the system is restarted after fixing the problem. At the present I’m just trying to turn on the error LED in order to show that the function call is working.

Adding the never-true while loop that you mentioned will hopefully handle the return problem, but I can’t tell because the compiler bombs before getting that far…

My pseudocode is intended to be something like this

loop {

detect error
set error code
call error function, passing error code to it }

error function {
return mechanics to “safe state”
turn off status good LED
blink error LED according to error code
repeat }

My test sketch for this is the following:

   // DEBUG SWITCH VARIABLE
    const int debugPin = 6;
    int debug = 1;

    // LED indicator variables
    const int runledPin =  7;    // the number of the RUN LED pin
    int runledState = LOW;       // Run LED state
    const int errorledPin =  8;  // the number of the ERROR LED pin
    int errorledState = LOW;     // Error LED state
    int error = 0;             // Error has happened (if high)
    int errorCode = 0;           // what is wrong
    
    
void setup() {
        // DEBUG SWITCH SETUP - NOTE switch ON = 0
        pinMode(debugPin, INPUT_PULLUP);
        debug = digitalRead(debugPin);

        // initialize serial communication at 9600 bits per second (only needed
        // for debugging):
        if (debug == 0) {
            Serial.begin(9600);
        }
        // LED pin setups        
        pinMode (runledPin, OUTPUT);   // RUN LED
        pinMode (errorledPin, OUTPUT); // ERROR LED
    } // SETUP END

void loop() {
        // Turn on RUN LED
        runledState = HIGH;
        digitalWrite(runledPin, runledState);

        
        if (0 == debug) {
            delay(1000);
        }

//  Basic function test - LED turns on when expected
//        errorledState = HIGH;
//        digitalWrite(errorledPin, errorledState);



// errorFunction test - currently not doing a test, just trying to go straight into function!

        errorCode = 1;
       error = errorFunction;
        
    } // END LOOP function

void errorFunction (errorCode) {

        // Out of loop, turn off run LED
        runledState = LOW;
        digitalWrite(runledPin, runledState);
  
        // Something is wrong, turn on error LED
        errorledState = HIGH;
        digitalWrite(errorledPin, errorledState);
        
        while (true)
        {}
        
    
    } // END errorFunction

This gives me compile errors:

error_function_test:0: error: variable or field ‘errorFunction’ declared void
error_function_test:0: error: ‘errorCode’ was not declared in this scope
error_function_test.cpp: In function ‘void loop()’:
error_function_test:46: error: ‘errorFunction’ was not declared in this scope
error_function_test.cpp: At global scope:
error_function_test:50: error: variable or field ‘errorFunction’ declared void

If I change the calling lines to:

        // errorFunction test
        errorCode = 1;
       error = errorFunction (errorCode);
        
    } // END LOOP function

void errorFunction (errorCode) {

I get the same error messages from the compiler. I don’t quite get it, since I declared “errorCode” as a global variable (I think) so it should be in all scopes…

If I change to get rid of the variable mentions (since errorCode is global, maybe I don’t need to explicitly mention it?) like this:

        // errorFunction test
        errorCode = 1;
       error = errorFunction ;
        
    } // END LOOP function

void errorFunction() {

I get

error_function_test.cpp: In function ‘void loop()’:
error_function_test:46: error: invalid conversion from ‘void (*)()’ to ‘int’

The sample code that I’ve found doesn’t seem to help me at all

Going back over the code for the umpteenth time, I noticed I had defined “error” as a global int variable, which might have accounted for the above, so I changed the calling name to “glitch”:

        // errorFunction test
        errorCode = 1;
       glitch = errorFunction ;
        
    } // END LOOP function

void errorFunction() {

I got:

error_function_test.cpp: In function ‘void loop()’:
error_function_test:46: error: ‘glitch’ was not declared in this scope

If I tried adding a definition for “glitch” up in my global variable list,

    int error = 0;             // Error has happened (if high)
    int errorCode = 0;           // what is wrong
    void glitch;

I got

error_function_test:11: error: variable or field ‘glitch’ declared void
error_function_test.cpp: In function ‘void loop()’:
error_function_test:46: error: ‘glitch’ was not declared in this scope

I am running out of ideas - HELP!

ex-Gooserider

You decided to ignore the first few things I said last time, didn’t you? I won’t repeat them, please re-read my earlier reply. This compiles without errors:

// DEBUG SWITCH VARIABLE
    const int debugPin = 6;
    int debug = 1;

    // LED indicator variables
    const int runledPin =  7;    // the number of the RUN LED pin
    int runledState = LOW;       // Run LED state
    const int errorledPin =  8;  // the number of the ERROR LED pin
    int errorledState = LOW;     // Error LED state
    
    
void setup() {
        // DEBUG SWITCH SETUP - NOTE switch ON = 0
        pinMode(debugPin, INPUT_PULLUP);
        debug = digitalRead(debugPin);

        // initialize serial communication at 9600 bits per second (only needed
        // for debugging):
        if (debug == 0) {
            Serial.begin(9600);
        }
        // LED pin setups        
        pinMode (runledPin, OUTPUT);   // RUN LED
        pinMode (errorledPin, OUTPUT); // ERROR LED
    } // SETUP END

void loop() {
        // Turn on RUN LED
        runledState = HIGH;
        digitalWrite(runledPin, runledState);

        
        if (0 == debug) {
            delay(1000);
        }

//  Basic function test - LED turns on when expected
//        errorledState = HIGH;
//        digitalWrite(errorledPin, errorledState);



// errorFunction test - currently not doing a test, just trying to go straight into function!

      errorFunction (1);
        
    } // END LOOP function

void errorFunction (int errorCode) {

        // Out of loop, turn off run LED
        runledState = LOW;
        digitalWrite(runledPin, runledState);
  
        // Something is wrong, turn on error LED
        errorledState = HIGH;
        digitalWrite(errorledPin, errorledState);
        
        while (true)
        {}
        
    
    } // END errorFunction

To blink the LED x times, where x is the error code, would involve doing a bit of a “blink loop” inside the “while true” loop.

Many thanks - that got me straightened out on this… It compiles fine on my system as well, and on the first run is doing what I expected.

I wasn’t “ignoring” what you said, it was more a case of my not understanding you - I’ve gone back and looked at your earlier post several times, and I’m still not sure I get it…

It certainly doesn’t help that the Playground tutorials and sample code doesn’t seem to look like this, presumably because they are using examples that return stuff - seems like they need a sample for a “void function” as well… (I do try and figure this stuff out myself before asking for help…)

Am I correct in assuming that the “1” in the errorFunction (1); line is the value for my error code? Presumably I’d then change it to a different value at each place the function was called?

At any rate, I’m going to be working on that blink function next - so far I have (using the sample code in part) gotten a single blink frequency, but I haven’t quite figured out the way to get a “blink n times, pause, repeat” function going. Will give it a few more hours of effort… :cold_sweat:

Thanks again,

ex-Gooserider

ex-Gooserider:
Am I correct in assuming that the “1” in the errorFunction (1); line is the value for my error code?

Yes.

seems like they need a sample for a "void function" as well..

You mean like "setup()" and "loop()"? ;)