Pages: [1]   Go Down
Author Topic: Calling function from variable  (Read 627 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
typing("abcdef");

This is at the beginning of my loop statement.


Code:
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));
  Serial.println(mess.charAt(0));
  "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.
  //Serial.println(counter);
  counter++;
} while (counter < mesLen);

//Serial.println("done");

}
void ta() {
  Serial.println("aaaaa");
}
void tb() {
 
}
void tc() {
 
} // And all throughout the alphabet :-)

Please let me know if you have any questions  smiley
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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].

Logged

NC, USA
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Delta_G

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

I tried this,

Code:
char c[] = "abcdef";

 myFPArray[c[3]]();
In loop.

Code:
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
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 16, 2013, 09:38:28 pm by lloyddean » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
« Last Edit: January 17, 2013, 02:01:03 am by lloyddean » Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: January 16, 2013, 10:07:22 pm by Professor Chaos » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your help, now I have somethings to try  smiley-grin
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Pages: [1]   Go Up
Jump to: