Using array to list all functions

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.

Thank you for your help.

Here's one way:

void loop() {

  switch (barChoice) {
    case 0:
      chestLedStack();
      break;

    case 1:
      chestLedStackRev();
      break;
      .
      .
      .
      .
      .
    case 7:
      chestLedCtrOut();
      break;

    default:
      break;
  }
}

Another way would be an array of function pointers.

Thank you. How would I do the array of function pointers, and then, which option would be the best method to implement?

With my original, the switch/case method, and the array pointers, that is 3 options, trying to make sure I develop good habits for future use.

void chestLedStack(), chestLedStackRev(), chestLedRandom(), chestLedKitt(), chestLedFill(), chestLedFillRev(), chestLedOutCtr(), chestLedCtrOut();

void (*pointerArray[])()  = {chestLedStack, chestLedStackRev, chestLedRandom, chestLedKitt, chestLedFill, chestLedFillRev, chestLedOutCtr, chestLedCtrOut};

void loop() {
  pointerArray[barChoice]();
}

“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”.

How good are you with pointers?

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.

Wait till you have lots of cases and your switch grows out of hand :slight_smile: Calling a function using an array of function pointers is basically a one-liner.

An old (and basically still applicable) rule is that a function, printed on a dot-matrix printer, should fit on a A4 page.

On the other hand, you should use what you understand.

The upside with this sketch, I don't have to have it perfect until August 1st. So I have time to learn the best way and learn it correctly.

gfvalvo:

void chestLedStack(), chestLedStackRev(), chestLedRandom(), chestLedKitt(), chestLedFill(), chestLedFillRev(), chestLedOutCtr(), chestLedCtrOut();

void (*pointerArray[])()  = {chestLedStack, chestLedStackRev, chestLedRandom, chestLedKitt, chestLedFill, chestLedFillRev, chestLedOutCtr, chestLedCtrOut};

void loop() {
 pointerArraybarChoice;
}

@gfvalo, I am trying to use this version (learn new stuff when I can), and I am getting a compile error.
I added

void (*pointerArray[])()  = {chestLedStack, chestLedStackRev, chestLedRandom, chestLedKitt, chestLedFill, chestLedFillRev, chestLedOutCtr, chestLedCtrOut};

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.