Go Down

Topic: Calling function from variable (Read 1 time) previous topic - next topic


Hello, I'm trying to create a function that will take a string called "message" and split the message up and display it one letter at a time (which I have already figured out and it is working) then from each of the letters it gets, run a function "t[the letter found]();" I know that I could make 26 if statements to do this, but I already have 500 lines of code, so I don't want to do that if I could. Is there any way to do this, or is it not possible? Any help would be appreciated, see code for more details.

Code: [Select]
This is at the beginning of my loop statement.

Code: [Select]
void typing(String message) {
  int mesLen;
  String mess;
  int counter = 0;
  mesLen = message.length();
  do {
  mess = message.substring(counter); 
  //Serial.println(mess = message.substring(counter));
  "t" && mess.charAt(0) && "();";  // I tried this, it was just a wild guess, and it just keeps returning either "1" or "l" im not really sure.
} while (counter < mesLen);


void ta() {
void tb() {
void tc() {
} // And all throughout the alphabet :-)

Please let me know if you have any questions  :)


Those characters are stored in the memory as binary numbers.  You could use them directly to access an array.  But you'd need to shift to zero.

Assuming you have an array of function pointers called myFPArray[]
Code: [Select]

char c[] = "abcdef";

myFPArray[c[3] - 'a']();

This would use c[3] where the 3 could be any variable, it indicates the position you want the letter from.  c[3] would be the 'd' character out of abcdef.  Then it subtracts the character 'a' from it (look up how ascii works) to get the number 3 and would call the function pointed to by myFPArray[3].

If at first you don't succeed, up - home - sudo - enter.


The only way I can think of is using a vector of function pointers and referencing the vector using an ENUM of the alphabet as the index.

However, this may qualify for a redesign of what you actually want to accomplish.  Figure out what you want the end result to actually be and ask it to the brains here. 

Just saw I was beaten to the punch.

Later, Dave



Could you explain more about how you would set this up in the code?

I tried this,

Code: [Select]
char c[] = "abcdef";


In loop.

Code: [Select]
void myFPArray() {
  Serial.println("Write something back");

After the loop function.

Is that what you meant when you said "assuming you have an array of function pointers called myFPArray[]"?
I'm not very experienced with arrays or more complicated coding, so i was kind of lost in what you said.

Thanks again


Jan 17, 2013, 03:34 am Last Edit: Jan 17, 2013, 03:38 am by lloyddean Reason: 1
Two questions -

o - Why call a separate function for each character.

o - Could you please provide a more complete description of what you want to accomplish.


I'm trying to be able to type in a message, that the code would then separate by letter, which would then tell my hexapod to move its front two arms and type out the corresponding message on a keyboard. Because I am using an SSC-32 Servo controller, all the arduino needs to do is send serial messages to it. So what I did was create functions that have text like this,
Code: [Select]
move2(mlt, 1600, mlf, 1600, 500, 0); // Move the left middle leg upward at a slow speed. which then talks to another function "move2" and it converts this into code that the SSC-32 accepts.

I was going to create the movements for the front two arms in 26 functions a-z, then have this code that would look through the message and activate the needed letters. The reason I separated each function was to keep the code from being too confusing and jumbled.

Hope that answered your question :)


Jan 17, 2013, 04:02 am Last Edit: Jan 17, 2013, 08:01 am by lloyddean Reason: 1
Here is a example of what I interpreted your original question as.  Perhaps you can take it and turn it into something you find useful.

You'll need to add the appropriate include files.

Code: [Select]

#define ENTRIES(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

typedef void (*function_ptr)(void);

void Z()    {}
void Y()    {}
void X()    {}
void W()    {}
void V()    {}
void U()    {}
void T()    {}
void S()    {}
void R()    {}
void Q()    {}
void P()    {}
void O()    {}
void N()    {}
void M()    {}
void L()    {}
void K()    {}
void J()    {}
void I()    {}
void H()    {}
void G()    {}
void F()    {}
void E()    {}
void D()    {}
void C()    {}
void B()    {}
void A()    {}

function_ptr functions[] = { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z };

void loop()
    char* message   = "Greetings";
    char* ptr       = message;
    char  ch;

    while ( ch = toupper(*ptr++) )
        if ( isalpha(ch) )
            (*functions[ch - 'A'])();

void setup()

EDIT: Modified to filter dispatchable characters

Professor Chaos

Jan 17, 2013, 04:05 am Last Edit: Jan 17, 2013, 04:07 am by Professor Chaos Reason: 1
Are 'mlt' and 'mlf' integer constants?  If so, I would use Mikal Hart's Flash library and create a table that holds your values:

Something like:

Code: [Select]
FLASH_TABLE(int, servovalues, 6, {mlt, 1600, mlf, 1600, 500, 0}, {values for b}, {values for c}, etc... );

That is, you have a table of 26 rows, each row containing the parameters corresponding to a particular character.  Then just look up the appropriate row in the table based on the character received, and pass those values to move2.


Thanks for your help, now I have somethings to try  :D


Be careful that you messages consist of alphabetic characters only or bad thing can happen!


Be careful that you messages consist of alphabetic characters only or bad thing can happen!

That's why I think the BEST idea is a switch / case or a set of if statements.  But the OP wanted something different. 
If at first you don't succeed, up - home - sudo - enter.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131