Go Down

Topic: Call dynamically created function name (Read 1 time) previous topic - next topic

rediculum

Jan 04, 2013, 09:22 am Last Edit: Jan 04, 2013, 09:28 am by rediculum Reason: 1
Hi

I'm trying this:

Code: [Select]

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: [Select]

 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
Regards
ReDiculum

Project:
LASERino

WizenedEE

Code: [Select]

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

rediculum

#2
Jan 04, 2013, 11:20 am Last Edit: Jan 04, 2013, 11:26 am by rediculum Reason: 1
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.
Regards
ReDiculum

Project:
LASERino

AWOL

You don't have enough elements in "funcs" - "rand(4)" returns 0..3 inclusive.
Otherwise, the code is fine.
"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.

rediculum

#4
Jan 04, 2013, 12:43 pm Last Edit: Jan 04, 2013, 12:45 pm by rediculum Reason: 1

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?
Regards
ReDiculum

Project:
LASERino

AWOL

#5
Jan 04, 2013, 12:48 pm Last Edit: Jan 04, 2013, 12:51 pm by AWOL Reason: 1
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: [Select]
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.
"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.

rediculum

#6
Jan 04, 2013, 12:52 pm Last Edit: Jan 04, 2013, 12:56 pm by rediculum Reason: 1
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.
Regards
ReDiculum

Project:
LASERino

AWOL

#7
Jan 04, 2013, 12:54 pm Last Edit: Jan 04, 2013, 01:02 pm by AWOL Reason: 1
Quote
On your hardware?

Running here on a 168-based Duemilanove.

Quote
If yes, the http://www.arduino.com.au/Simulator-for-Arduino.php is crap

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

dhenry

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.

rediculum


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  XD :D

Anyway thanks for all the help
Regards
ReDiculum

Project:
LASERino

PeterH


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: [Select]

// uncompiled, untested
switch(random(4))
{
   case 0:
       func0();
       break;
   case 1:
       func1();
       break;
   case 2:
       func2();
       break;
   case 3:
       func3();
       break;
}
I only provide help via the forum - please do not contact me for private consultancy.

robtillaart

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: [Select]

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 ;)
Rob Tillaart

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

Go Up