Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
Thread Tools Search this Thread
11-03-2012, 22:51   #1
magicianz
Registered User
 
Join Date: Feb 2009
Posts: 989
Arduino - Array problem

I'm currently doing a project that takes an input from the serial monitor in the processing compiler, converts it into a pulse sequence and transmits that to a distant receiver. It's quite easy to accomplish it with just a great dear of repetitive code but I hate doing that. The problem I'm currently having is that I want to make an array which contains numerous methods, but I cannot figure out how to do it!

E.g. Float array[] = { method1(), method2(), ....... } ;

I am constantly getting error messages with identifier I try. Im looking to find this out as it cuts out about 70 - 80 lines of repetitive stuff.

For anyone who doesn't know what processing is, it's a C based language used to programme the arduino micro-controllers.

Thanks!
magicianz is offline  
Advertisement
12-03-2012, 11:30   #2
Tillotson
Registered User
 
Join Date: Sep 2008
Posts: 209
If the language is close to C, then your type signatures are probably wrong.

Code:
float sum(float a, float b)
{
        return a + b;
}

/* fpa is an array of pointers to (float, float) functions which return float */
float (*fpa[])(float, float) = { sum, sum };
Tillotson is offline  
12-03-2012, 11:33   #3
Groinshot
Registered User
 
Join Date: Sep 2009
Location: Dublin
Posts: 1,039
I have experience with C, and Arduino, but am more than prepared to be pulled up on this..
Can't just create an array of functions, it doesn't work. What you need to do is create an array of pointers, one pointer to each function, and store each pointer as a slot in the array. You can then call each function by dereferencing the pointer.....
See SO if that doesn't make any sense.....
However, I've never tried it on an arduino, so don't know if it will work or not.
Groinshot is offline  
Thanks from:
12-03-2012, 12:15   #4
Tillotson
Registered User
 
Join Date: Sep 2008
Posts: 209
Quote:
Originally Posted by Groinshot View Post
What you need to do is create an array of pointers, one pointer to each function, and store each pointer as a slot in the array. You can then call each function by dereferencing the pointer.
Exactly, the above code could be written:
Code:
float (*fpa[])(float, float) = { &sum, &sum };
To invoke these functions:
Code:
printf("%f\n", fpa[0](1.0, 2.0));
or
Code:
printf("%f\n", (*fpa[0])(1.0, 2.0));
I prefer the shorter syntax but the other might be clearer. It's interchangeable either way.

Last edited by Tillotson; 12-03-2012 at 18:34. Reason: typo
Tillotson is offline  
(2) thanks from:
14-03-2012, 14:27   #5
magicianz
Registered User
 
Join Date: Feb 2009
Posts: 989
Thanks a lot to both of ye. Only really got to look at it properly today and, for the moment, its working perfectly!
Code:
 float (*arrayName[]) (float,float) = { func1, func2, func3,.....};
That works perfectly for me, just took a while to properly understand what you were saying. Thanks again! Saved me a lot of stress, none of the examples i've seen online are as clear
magicianz is offline  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet