Call variables with arduino variable calculation

How can I call multiple functions or variables with calculations?

What I need is something like:

void 1() {
  orden to execute
}

void 2() {
  orden to execute
}

void 3() {
  orden to execute
}

I need an order that for example with

pattern = 3221;

I ran (void 3), (void 2) twice, and (void 1). And so with any combination of numbers to pattern.

something like this you mean?

//array of pointer functions
typedef void (* functionPtrs) (void);

void zero()
{
  Serial.println("zero");
}

void one()
{
  Serial.println("one");
}

void two()
{
  Serial.println("two");
}

functionPtrs funcs[3] = {&zero, &one, &two};

void setup()
{
  char pattern[] = "0221"; //numbers here are ASCII characters
  int i = 0;
  Serial.begin(115200);

  while (pattern[i] != '\0') {
    funcs[pattern[i] & 0x0F](); //&0x0F to get number value out of ASCII value
    ++i;
  }
}

void loop()
{
  
}

hope that helps…

I went back and read a bunch of your posting history, wondering what is on your mind.

It seems to me like you have been trying for some time to solve the problem of building a dynamic chain of functions..

One function leads to the next etc. This it not easy to do because.. Its a bad idea. When a function is entered it eats up a chunk of RAM for its local variables and return address. This is why Arduino people fear/discourage recursive methods of writing code.

But.. What you could do, that would possibly solve your issue and keep things manageable, is write some sort of delegation function.

You would enter the delegation function with some sort of input you needed worked on. It would delegate/call the first sub function to deal with the first process of whatever you are doing. In that sub function, you return something that tells the calling delegator function what sub function to call next. On and on.

In this way you can chain together functions, but by returning to your delegator each time to be re-directed along your chain, you release each sub function's RAM along the way.

Does this make any sense? Or have I just missed the point entirely?

-jim lee

are you looking for something like this which invokes a specific function for each digit entered thru serial monitor

void setup () {
    Serial.begin (115200);
    Serial.println ("go");
}

void f1 (void) { Serial.println (__func__); }
void f2 (void) { Serial.println (__func__); }
void f3 (void) { Serial.println (__func__); }
void f4 (void) { Serial.println (__func__); }
void f5 (void) { Serial.println (__func__); }

void loop ()
{
    if (Serial.available ())  {
        switch (Serial.read())  {
        case '1':
            f1 ();
            break;

        case '2':
            f2 ();
            break;

        case '3':
            f3 ();
            break;

        case '4':
            f4 ();
            break;

        case '5':
            f5 ();
            break;

        default:
            break;
        }
    }
}

output for "3221"

go
f3
f2
f2
f1

jonhander12345:
What I need is something like:

Your example is so simple that it illustrates nothing useful about what is troubling you.

You have shown a series of functions that each seem intended to do the same thing.

And you have provided no indication of how you would like to use those functions.

...R

@sherzaad

Thanks for the example in reply #1, I have always struggled with pointers in C and C++. The concept is simple enough but it confuses me for some reason. I have taken your example and modified it and it still works, so I guess I must understand it.

++Karma;

PerryBebbington:
@sherzaad

Thanks for the example in reply #1, I have always struggled with pointers in C and C++. The concept is simple enough but it confuses me for some reason. I have taken your example and modified it and it still works, so I guess I must understand it.

++Karma;

I always thought of it like things with strings attached. The strings are the pointers.

You either have the thing, or follow the string.

-jim lee

So you're looking for naming custom functions according to varied calculations?

pon00009:
So you're looking for naming custom functions according to varied calculations?

No; they're looking to invoke the correct functions (of many functions) according to the sequence specified in some input.

leandra_reis:
No; they're looking to invoke the correct functions (of many functions) according to the sequence specified in some input.

The examples in the Original Post suggest to me that the OP does not himself know what he wants.

...R