system
November 6, 2010, 10:30pm
#1
My question is this, Is it posible to call some functions accoarding with the value of some variables?
for example lest supose we have these functions
void task_1(){
}
void task_2(){
}
void task_3(){
}
void task_4(){
}
void task_5(){
}
and we have these variables
int input[6];
when these variables have this content:
input[0]=5
input[1]=1
input[2]=1
input[3]=3
input[4]=2
input[5]=4
i want the program to execute first the task_5,then double time the task_1 then the task_3 then the task_2 then the task_4.
sorry for my bad english
system
November 6, 2010, 10:32pm
#2
PSEUDOCODE
loop for all inputs
switch (input)
case 1: task_1(); break;
case 2: tast_2(); break;
...
Or you could create an array of function pointers, but forget I said it ;)
system
November 6, 2010, 10:43pm
#3
can you tell me please a full example code?
void procesTasks()
{
for (int i=0; i< 6; i++) // assume there are 5 elements in input
{
switch(input[i])
{
case 1: task_1(); break;
case 2: task_2(); break;
case 3: task_3(); break;
case 4: task_4(); break;
case 5: task_5(); break;
default: break; // what to do with not supported values?
}
}
}
system
November 6, 2010, 11:03pm
#5
Cheers that was really simple
thanks alot, my mind was stucked!!!
Maybe it was just too late for programming ;)
system
November 7, 2010, 2:21pm
#7
weird but it doesnt works…it always prccess (loop) the first task
it works with if…then but i could prefer that way
system
November 7, 2010, 2:23pm
#8
There was no "loop" in the example.
Maybe if you showed us what you have written, we could help.
system
November 7, 2010, 2:38pm
#9
int order_task[20];
void procesTasks()
{
for (int i=0; i< 20; i++)
{
switch(order_task[i])
{
case 1: task_1(); break;
case 2: task_2(); break;
case 3: task_3(); break;
case 4: task_4(); break;
case 5: task_5(); break;
default: break;
}
}
}
void order_task_data(){
order_task[0]=5;
order_task[1]=5;
order_task[2]=4;
order_task[3]=3;
order_task[4]=2;
order_task[5]=1;
order_task[6]=0;
order_task[7]=0;
order_task[8]=1;
order_task[9]=2;
order_task[10]=3;
order_task[11]=4;
order_task[12]=5;
order_task[13]=6;
order_task[14]=0;
order_task[15]=1;
order_task[16]=2;
order_task[17]=3;
order_task[18]=4;
order_task[19]=5;
}
void loop() {
order_task_data();
procesTasks() ;
}
it is running all time the task_1…
with if then else the same (i was wrong before) :-[
system
November 7, 2010, 2:47pm
#10
Please use the button when posting code.
You do not declare any variables or functions in that code, neither do you process the same array in the for loop as you setup in order_task_data.
system
November 7, 2010, 2:52pm
#11
there is this on my code on the beginning
int order_task[20];
i can't paste all code, i am working on a big project
system
November 7, 2010, 2:54pm
#12
So where do you write values into your array?
Come on, help us here.
You can post enough of the code to show us what isn't working for you.
Simply posting an array declaration doesn't help at all.
If all the functions have identical protoypes, you can eliminate the switch, and just have an array of function pointers.
system
November 7, 2010, 3:08pm
#14
Try this, TOTALLY UNTESTED
void setup() {
order_task_data();
}
void loop() {
procesTasks() ;
}
void order_task_data(){
order_task[0]=5;
order_task[1]=5;
order_task[2]=4;
order_task[3]=3;
order_task[4]=2;
order_task[5]=1;
order_task[6]=0;
order_task[7]=0;
order_task[8]=1;
order_task[9]=2;
order_task[10]=3;
order_task[11]=4;
order_task[12]=5;
order_task[13]=6;
order_task[14]=0;
order_task[15]=1;
order_task[16]=2;
order_task[17]=3;
order_task[18]=4;
order_task[19]=5;
}
void procesTasks()
{
for (int i=0; i< 20; i++)
{
switch(order_task[i])
{
case 1: task_1(); break;
case 2: task_2(); break;
case 3: task_3(); break;
case 4: task_4(); break;
case 5: task_5(); break;
default: break;
}
}
}
void task_1() {}
void task_2() {}
void task_3() {}
void task_4() {}
void task_5() {}
Did you forget to read the thread before posting AWOL?
system
November 7, 2010, 3:21pm
#15
with this code (adove) it is running the tasks with this order:
task_6
task_2
task_1
task_3
.......
i'm gonna be crazy...
system
November 7, 2010, 3:23pm
#16
order_task[6]=0
There is no case 0, and no case 6 for that matter.
Did you forget to read the entire thread?
Maybe if your sketch is too large to post, you’ve got too much RAM usage, and things are getting screwed that way.
A little less forgiving of badly initialised “order_task”, but simpler:
void (*fn [])(void) = {NULL, task_1, task_2, task_3, task_4};
void processTasks ()
{
for (int i = 0; i < 20; ++i){
if (fn [order_task[i]]) {
fn [order_task[i]] ();
}
}
}
system
November 7, 2010, 4:07pm
#17
Now it's time: post your entire sketch.
:)
system
November 7, 2010, 5:05pm
#18
the story continues…
void procesTasks()
{
for (int i=1; i<20; i++)
{
Serial.println(i);
delay(300);
switch(order_task[i])
{
case 1: task_1(); break;
case 2: task_2(); break;
case 3: task_3(); break;
case 4: task_4(); break;
case 5: task_5(); break;
case 6: task_6(); break;
case 7: task_7(); break;
default: break;
}
}
}
i added the line to see the value “i” takes and in serial monitor i got this:
1
2
123
196
197
198
199
200
201
202
203
204
205
206
207
208
209
…
!!! :o :o :o : : :
system
November 7, 2010, 5:08pm
#19
This has "insufficient RAM" written all over it.
80 byte buffers on the stack may not help.
system
November 7, 2010, 5:11pm
#20
[edit]what AWOL said[/edit]
http://arduino.cc/en/Main/Hardware
Click your board and see how much SRAM it has.