I am not sure if there is a way to do this, so I am asking the experts.
I have a sketch that utilizes a pushbutton to cycle through various LED states. I have sub-functions per LED state, and the sub-functions work perfect. My question is about the best way to cycle through them.
Currently, this is what my code looks like (reduced to show just the section in question for simplicity):
void loop(){
// ------ Debounce code to make sure one press = one step ------
barDebounce(); // Writes value to [barChoice] to make selection
// ------ Selection call section ------
if (barChoice == 0){
chestLedStack();
}
if (barChoice == 1){
chestLedStackRev();
}
if (barChoice == 2){
chestLedRandom();
}
if (barChoice == 3){
chestLedKitt();
}
if (barChoice == 4){
chestLedFill();
}
if (barChoice == 5){
chestLedFillRev();
}
if (barChoice == 6){
chestLedOutCtr();
}
if (barChoice == 7){
chestLedCtrOut();
}
}
This setup works. It does exactly what I ask of it. But there has to be a better way to cycle through these.
“Best” is too subjective. If the ONLY thing that needs to happen based on barChoice is to call the appropriate function, then I like the array. If other barChoice-dependent code needs to execute in loop(), then I like the switch().
But, that’s just what I LIKE. I don’t’ think either is “Best”.
barChoice is modified within a completely separate function called "barDebouce()" activated in the loop function. It is completely separate and outside of the list of functions that I wish to choose from.
Being good with pointers? Well I have been changing how I address variables to arrays now. I have a few sketches that run really well in a for loop, and I have gotten good with naming 2 or 3 arrays and then just addressing the location within the array instead of creating a variable for each instance.
The more I look over the "array" option vs the "switch" option vs the "if" option, I am leaning more towards the switch. I think it will be the easiest for my application.
before the setup function (in the header). And when I compile, I get an error: error: 'chestLedCtrOut' was not declared in this scope for each sub-function. Do I have something in the wrong place?
I'm an idiot. When I copied and pasted, I missed the first line. When I put it above the void (* chestArray[]) line it compiled.