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