Pages: [1]   Go Down
Author Topic: Call dynamically created function name  (Read 1148 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Newbie
*
Karma: 5
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I'm trying this:

Code:
String Func;
int Delay = 1000;

void setup() {
}

void loop() {
Func = "Func";
Func += random(1,4);
}

void Func1()
{
delay(Delay);
}
void Func2()
{
delay(Delay);
}
void Func3()
{
delay(Delay);
}

The value of the string "Func" is successfuly having a random number appended, but the function itself is not called (the delay is not executed). I cannot just execute
Code:
 Func;

Can anybody help me in creating dynamically function name calls with the random number? I trying to avoid to hardcode an array with the function names like here http://arduino.cc/forum/index.php/topic,40842.0.html

Thanks in advance
« Last Edit: January 04, 2013, 03:28:59 am by rediculum » Logged

Regards
ReDiculum

Project:
LASERino

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int Delay = 1000;

void Func1()
{
delay(Delay);
}
void Func2()
{
delay(Delay);
}
void Func3()
{
delay(Delay);
}

void (* funcs[])() = {Func1, Func2, Func3};

void setup() {

}

void loop() {
    digitalWrite(13, HIGH);
    funcs[random(4)]();
    digitalWrite(13, LOW);
    funcs[random(4)]();
}

Any text that isn't between quotes ("") are completely gone after the compiler runs. You need to use an array of function pointers, or something like a switch statement
Logged

Switzerland
Offline Offline
Newbie
*
Karma: 5
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your answer. Since I don't have my hardware yet I tested your code in the Simulator and it does not work. It says "funcs[random(4)]();" is an unknown command.
« Last Edit: January 04, 2013, 05:26:29 am by rediculum » Logged

Regards
ReDiculum

Project:
LASERino

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't have enough elements in "funcs" - "rand(4)" returns 0..3 inclusive.
Otherwise, the code is fine.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Switzerland
Offline Offline
Newbie
*
Karma: 5
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't have enough elements in "funcs" - "rand(4)" returns 0..3 inclusive.
Otherwise, the code is fine.
Doesn't look like, I got the same error even if I set the exact amount of elements -> random(3) (which is 0-2)

But back to main topic: So there is no way to call a function name which as been created dynamically by a string?


* err.jpg (15.27 KB, 775x168 - viewed 11 times.)
« Last Edit: January 04, 2013, 06:45:40 am by rediculum » Logged

Regards
ReDiculum

Project:
LASERino

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Doesn't look like, I got the same error even if I set the exact amount of elements
Then I would suggest the error is the fault of the simulator.

The following code appears to work exactly as expected
Code:
void Func0()
{
  Serial.print ('G');
}
void Func1()
{
  Serial.print ('A');
}
void Func2()
{
  Serial.print ('T');
}
void Func3()
{
  Serial.print ('C');
}

void (* funcs[])() = {Func0, Func1, Func2, Func3};

void setup()
{
  Serial.begin (9600);
}

void loop()
{
    funcs[random(4)]();
    delay(1000);
}

Quote
But back to main topic: So there is no way to call a function name which as been created dynamically by a string?
I can't think of a compiled language it would be possible with.

Tell us what you want to do, not how you think it should be done.
« Last Edit: January 04, 2013, 06:51:52 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Switzerland
Offline Offline
Newbie
*
Karma: 5
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On your hardware? If yes, the http://www.arduino.com.au/Simulator-for-Arduino.php is crap

Tell us what you want to do, not how you think it should be done.
I want call functions randomly.
« Last Edit: January 04, 2013, 06:56:14 am by rediculum » Logged

Regards
ReDiculum

Project:
LASERino

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
On your hardware?
Running here on a 168-based Duemilanove.

Quote
I've never used it or any other simulator, so I cannot comment.

Quote
I want call functions randomly.
That's exactly what has been demonstrated.
« Last Edit: January 04, 2013, 07:02:23 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
   
Call dynamically created function name

I have a better idea: rather than just dynamically create function names, dynamically create a compiler that compiles your dynamically created code that is dynamically flashed into a dynamically powered arduino so that it dynamically responds to inputs that are dynamically applied to dynamically assigned pins with dynamically changing frequencies and dyanmically altered amplitude.

All dynamically done, all the way.
Logged

Switzerland
Offline Offline
Newbie
*
Karma: 5
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
   
Call dynamically created function name

I have a better idea: rather than just dynamically create function names, dynamically create a compiler that compiles your dynamically created code that is dynamically flashed into a dynamically powered arduino so that it dynamically responds to inputs that are dynamically applied to dynamically assigned pins with dynamically changing frequencies and dyanmically altered amplitude.

All dynamically done, all the way.

Ok, I got it  smiley-lol smiley-grin

Anyway thanks for all the help
Logged

Regards
ReDiculum

Project:
LASERino

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can anybody help me in creating dynamically function name calls with the random number? I trying to avoid to hardcode an array with the function names like here

I don't understand why you want to avoid using an array of function pointers. It is a perfectly reasonable way to achieve what you're asking for.

An alternative, which takes more code but avoids using those scary function pointers, is to use a switch:

Code:
// uncompiled, untested
switch(random(4))
{
    case 0:
        func0();
        break;
    case 1:
        func1();
        break;
    case 2:
        func2();
        break;
    case 3:
        func3();
        break;
}
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13734
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But back to main topic: So there is no way to call a function name which as been created dynamically by a string?
on contrary you can work around it and (sort of) call by name but it cost you extra overhead.

Code:
void callFuncByName(char *name, int param)
{
  if (strcmp(name, "func0")==0) func0(param);
  else if (strcmp(name, "func1")==0) func1(param);
  else if (strcmp(name, "func2")==0) func2(param);
  else if (strcmp(name, "func4")==0) func4(param);
}
Of course you can iterate over an array of function names or over an array of structs that contain the name and the functionpointer etc.

And yes comparing the reverse name would fail much faster smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: