I'm stuck call Function from char array

Hi :smiley: i look around in google like 4 hs i give up T.T

I have a Sketch (i put a piece of the sketch here)

char vel[3] = {'vel_1','vel_2','vel_3'};


 void vel_1() {     // Funcion
  delay(50);
  digitalWrite(ledPin[Led], HIGH);
  delay(50);
  }
  void vel_2() {     // Funcion
  delay(150);
  digitalWrite(ledPin[Led], HIGH);
  delay(150);
  }
  void vel_3() {     // Funcion
  delay(325);
  digitalWrite(ledPin[Led], HIGH);
  delay(325);
  }

i whant to call a function like this

vel[1] // and this call the name f the function on the array in this case vel_2

i found this 6 months ago but i can t find it again T.T how the hell i can convert "vel[1]" to "vel_2" i remember is like "this.vel[1]" but i get an error

flash5.cpp: In function 'void loop()':
flash5:26: error: invalid use of 'this' in non-member function

"invalid use of 'this' in non-member function"

plz HELP! T.T

thx and sry if i ask for something too easy T.T

you need to make an array of pointers to functions, rather than an array of function names (after all, function names are never seen by the actual cpu)

void f0() { Serial.println("f0"); }
void f1() { Serial.println("f1"); }
void f2() { Serial.println("f2"); }
void f3() { Serial.println("f3"); }

void (*funcs[])(void) = {f0, f1, f2, f3};

void setup() {
    Serial.begin(115200);
}
void loop(){
    Serial.println("Give a number");
    while (Serial.available() == 0);
    char c = Serial.read();
    funcs[c-'0']();
}

mmm i understand what u are saying, but i do this in a easy way like "this is a function" i whant to convert/declare this string is the nam of a function, i can do it like you say, but that is unpractical and i do this, because i whant to do something like this

void loop(){
  
  digitalWrite(ledPin[0], LOW);
  digitalWrite(ledPin[1], LOW);
  (Something that convert the next value of the array into function name).vel[Phase]();

}

where Phase is a number betewn 0 and 5, and it change if i pres a buttom

if i put in the sketch "vel_1" (this is the name of the function) all work great but how i can convert the value of the array (that is a string) in to fuction? xD

this is a comand or something in action scrip is this.[string] or something like that and in C i something similar :stuck_out_tongue: that is what i whant xD

THX :smiley:

fenix8k:
mmm i understand what u are saying, but i do this in a easy way like "this is a function" i whant to convert/declare this string is the nam of a function, i can do it like you say, but that is unpractical and i do this, because i whant to do something like this

The microcontroller has no idea was val_1 is, so if you want to make a link between val_1() and "val_1", you need to use a function pointer.

i kanw The microcontroller has no idea was vel_1 (vel_1 is the actual name of a fucntion :p) is, but i whant to tell him that the next chars are the name of a function, like This is the name of a function.(chars)

i do whant i saying 6 months ago but i can t remember the exact way to do it, and arduino is saying me that i mystake something when i put in the sketch "this.vel[0]" someone can put a link or something of how to use "this." i can t fin it in google T.T

"this" is used in a class and is a pointer to the "current" instance of the class.

You can't access the names of anything once it's been compiled and uploaded.

OP is there a section in the International part of the forum that can help?
It's fairly clear English is not your first language, and there may be a better way of posing the question.

When the compiler gets done with your code, the name vel_1 of the function vel_1() no longer exists. If you want to receive a string over the serial port, and call the function at the address that corresponds to the name, you have to do something like this:

char funName[80];
int index;

// populate funName from serial data

if(strcmp(funName, "vel_1") == 0)
{
   vel_1();
}
else if(strcmp(funName, "vel_2") == 0)
{
   vel_2();
}

fenix8k:
i found this 6 months ago but i can t find it again T.T how the hell i can convert "vel[1]" to "vel_2" i remember is like "this.vel[1]" but i get an error

There are some scripting languages that look similar to 'C' which support this, but 'C' doesn't. The only solution would involve writing your own look-up table or hard-coded string comparisons to decide which function to call based on the content of a variable.

It might help if you could describe what your sketch is supposed to do - there may well be ways to do it that can avoid your need for an array of function pointers entirely.

wildbill:
It might help if you could describe what your sketch is supposed to do - there may well be ways to do it that can avoid your need for an array of function pointers entirely.

Hi, the purpose of my sketch is with 3 buttons control the Blink speed of a led, second buttom change the current led to blink and the last buttom turn of and of the leds without stoping the blink function

i make the sketch and it works great but i have very litle space to code and i whant to make the code as small as possible.

i think another solution to calls functions in the whay i whant

i can use

  if (state == 0) {
                 function_1();
               }
            if (statae == 1) {
                 function_2();
               }

Ths for help me

sorry for my spelling i m at work and my keyboard work pretty bad xD and English is not my native language