First off, I'm a complete noob as I've mentioned in another thread. I've got a couple weeks of experience during which all of my knowledge has come from dissecting and modifying other people's code. For my current question I've been searching the forum and found a lot of posts that have me convinced i need to use pointers to do what I want but it seems that all of those posts deal only with void functions, nothing that actually returns a value of any kind. I (wrongly) assumed that I could simply change "void" to "int" to get my called function to return a value but that results in compiler errors. I don't actually know anything about pointers other than what little bits I've managed to understand from reading other posts.
My application:
i'm working on an imu based leveling system but i can't ensure that the imu will be mounted with the Z axis vertical. I'm dealing with this uncertainty by using two inputs driven high or low to tell the program what the imu's orientation is. I could certainly run an IF statement in void loop() to determine the state of the inputs and select the appropriate function. I realize that doing so would take very little processor time or effort but there's just no reason to run that check every time the program loops when i could run the check once at startup, set a variable based on the result, and use that variable to call the necessary function.
I've attached my test sketch below. Any help getting it to work would be greatly appreciated and layman's explanations of how your code is written and why it works would be even better. Thanks all.
int (*myFunc[])(int)={&five, &eighteen, &fortytwo, &eleven};
int val = 0;
int progSelect = 0;
const int switchA = 13;
const int switchB = 12;
void setup() {
// put your setup code here, to run once:
pinMode (switchA, INPUT);
pinMode (switchB, INPUT);
Serial.begin(9600);
if (switchA == HIGH && switchB == HIGH) {
progSelect = 0;
}
else if (switchA == HIGH && switchB == LOW) {
progSelect = 1;
}
else if (switchA == LOW && switchB == LOW) {
progSelect = 2;
}
else if (switchA == LOW && switchB == HIGH) {
progSelect = 3;
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(val);
Serial.println();
myFunc[progSelect]();
delay(1000);
}
int five() {
val = 5;
return val;
}
int eleven() {
val = 11;
return val;
}
int fortytwo() {
val = 42;
return val;
}
int eighteen() {
val = 18;
return val;
}
For reference the error i'm getting is this:
1:59: error: invalid conversion from 'int ()()' to 'int ()(int)' [-fpermissive]
and
32:22: error: too few arguments to function