loops

hey
i was wondering how i could programme arduino to loop sommeting indefinitely unless it gets a command to loop something else
for example:
i have 3 leds on pin 9-11 and an ir reciver
how can a state: "if i recieve command "1", then furst led 9 goes on then led 10 on then 9 off then 11on and repeat this until the reciever recieved an other command lest say command "2" and beging an other order"

I did this with my xmas lights display. Basically the structure is something like this:

Create a function, say cmd_one() and write what you want the leds to do in it.
Create another one, say cmd_two(), and write your next sequence in it.
Create a third function called do_task() which calls either cmd_one() or cmd_two() based on a variable, let's call it rf_cmd.

Now, in your main loop you are going to do two thing, a) check for an rf signal, and b) call do_task(). If you received an rf signal, set the variable rf_cmd accordingly and pass is to the do_task() function which in turn calls the correct cmd function. As long as you don't get a new rf signal, that variable remains the same, so do_task() continues to call the same cmd_* function.

When you receive a new rf signal, change the variable rf_cmd and pass it on again. Now do_task() calls a different function based on that.

The idea here is to not use delay() anywhere in your code, including the cmd_one and cmd_two functions. Take the example of BlinkWithoutDelay and use it. Your main loop runs constantly and checks for an incoming rf signal constantly. It also calls do_task() constantly which in turn calls your LED sequence (cmd_one or cmd_two).

Like I said, I used this with my xmas light with one rf module sending sequence changes to 14 other modules, telling them what sequence to change to. As long as no new signal was received, the same sequence kept playing because the do_task() command kept calling the same sequence over and over again.

Hope that helps.

could you send me the code that you used, plz?
it would help to see (and understand) the structure a lot

how do i create such a function sy cmd_one()

is it like this void (cmd_one) ?

// defining it:
void cmd_one( int number) {
// do something. You have the value in "number". You can not change it.
}

// using it
void loop() {
  cmd_one ( 5 ) ;
}

i tried sommething, im not sure what that number is for
but still this i tried
en get the error

#include "IRremote.h"

int RECV_PIN = 0;                // pin0  =linker poot reciever, middenste poot reciever= 5V, rechter poot reciever =grd
IRrecv irrecv(RECV_PIN);

decode_results results;

int brightness = 0;
int fadeAmount = 5;

void setup()  { 
 Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(9, OUTPUT);
} 

// defining it:

// using it
void loop() {
   if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
  cmd_one (  ) ;
   irrecv.resume(); 
 
  
}
}
void cmd_one( int number) {
// do something. You have the value in "number". You can not change it.
  if (results.value==0x8B77887) {
    //fadeAmount = -fadeAmount; 
    brightness = brightness - fadeAmount;
      delay(30);   
  };    
}

My example included a number in the call of the function. You are missing it. That is what the error is saying. It also says it is on line 23. (You can see which line number is positioned on right at the bottom)

Why did I include a numeric argument? Well, it might have been usefull to pass a number, and this way you got that syntax. If you do not want to pass a number you must omit it both in the declaration and the call.

i am sorry but i don't understand

My example included a number in the call of the function. You are missing it. That is what the error is saying. It also says it is on line 23. (You can see which line number is positioned on right at the bottom)

Why did I include a numeric argument? Well, it might have been usefull to pass a number, and this way you got that syntax. If you do not want to pass a number you must omit it both in the declaration and the call.

what does it meen, i have practicly never programed i know more or less what a functions is but i don't understaan what it have to ommit in the declaration, what ever a declaration is, is dhat the void cmd_one? or...

Passing a variable to the actual cmd_* function isn't necessary unless you are actually going to use it for the sequence. However, you do need a variable passed to the main do_task() function as that determines WHICH cmd_* function it's calling.

The jist of the code is something like this:

void cmd_one() {
  // do one LED sequence here
}

void cmd_two() {
 // do another sequence here
}

void loop() {
 // check for incoming RF data
  if (new rf data) {
    set 'num' variable to whatever sequence that data refers to
  }
  do_task(num);
}

void do_task(num) {
  switch (num) {
    case 1:
      cmd_one();
      break;
    case 2:
      cmd_two();
      break;
  }
}

I'll post more details later. I'm rather busy at the office right now.

Just realized I still have 2 meetings to get through here and it's already past 5pm ... lovely. Anyway, I forgot to mention, the pseudo code, as written will run cmd_* constantly, so you have to work in your delays accordingly, but without using the delay() command. So something like this would work:

void cmd_one() {
  If (millis() - lastRun > 50) {
    // blinks a led, or do whatever
    lastRun = millis();
  }
}

Now, that command will only fire every 50ms, eventhough the do_task() command is calling it constantly.

Hope that makes sense.

i tried sommething and it gives the error variable er field 'do_task' declared void.

i think my problem is in the loop... i don't know how to change/give num a value (that is the intention or am i completely wrong?)

#include "IRremote.h"


int RECV_PIN = 0;               
IRrecv irrecv(RECV_PIN);

int brightness = 0;    // how bright the LED is
int fadeAmount = 5; 

void setup()  { 
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
} 



void cmd_one() {
    // do one LED sequence here
    analogWrite(9, brightness); 
    brightness = brightness + fadeAmount; 
  If (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ; 
        }  
  lastRun = millis();
    }
}

void cmd_two() {
 // do one LED sequence here
    analogWrite(11, brightness); 
    brightness = brightness + fadeAmount; 
  If (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ; 
        }  
  lastRun = millis();
    }
}

void loop() {
         // check for incoming RF data
 if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value==0x8B77887) {
     num = 1  ;                          // here is the problem i think
    }
    
  do_task(num);
  
 }
}

void do_task(num) {
  switch (num) {
    case 1:
      cmd_one();
      break;
    case 2:
      cmd_two();
      break;
  }
}
void do_task(num)

Let's assume the type of "num" is an "int", because the compiler can't.
You need to tell it.

Then there's the problem that there's nowhere in "loop" that there's a variable called "num" in scope, but you still assign the value 1 to it..

And "if" is spelled "if", not "If".

how do i tell it's an int
and i called num in the loop "1" because i thought the loop would sellect case 1 in do_task (num)
but how do i do it correctly?

num = 1;

I don't see num variable being declare in your code.

Let's assume the type of "num" is an "int", because the compiler can't.
You need to tell it.

void do_task(int num)

#include "IRremote.h"


int RECV_PIN = 0;               
IRrecv irrecv(RECV_PIN);

int brightness = 0;    // how bright the LED is
int fadeAmount = 5; 

void setup()  { 
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
} 



void cmd_one() {
    // do one LED sequence here
    analogWrite(9, brightness); 
    brightness = brightness + fadeAmount; 
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ; 
        }  
  lastRun = millis();
    }
}

void cmd_two() {
 // do one LED sequence here
    analogWrite(11, brightness); 
    brightness = brightness + fadeAmount; 
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ; 
        }  
  lastRun = millis();
    }
}

void loop() {
 // check for incoming RF data
 if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value==0x8B77887) {
     num = 1  ;
    }
    
  do_task(num);
  
 }
}

void do_task(int num) {
  switch (num) {
    case 1:
      cmd_one();
      break;
    case 2:
      cmd_two();
      break;
  }
}

and now i get the folowing errors (see image)

The error messages are fairly self-explanatory - you haven't told the compiler about the variables you are using, before you use them.

and how do i do that?

lastRun needs to be a long, so declare it at the top of your sketchlong lastRun; As for 'results', I don't see where that's being defined either. That's something you need to figure out from the IR library you're using.

i thanks for tha code with lastrun....
i solved that results thing to now only num is the problem, the error says " 'num' was not declaired in this scope" and num = 1 ; is marked and "do_task(num)" too
how declair i this (and wher)?

#include "IRremote.h"
#include "IRremoteInt.h"




long lastRun;
int RECV_PIN = 0;               
IRrecv irrecv(RECV_PIN);
decode_results results;

int brightness = 0;    // how bright the LED is
int fadeAmount = 5; 

void setup()  { 
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn();
} 



void cmd_one() {
    // do one LED sequence here
    analogWrite(9, brightness); 
    brightness = brightness + fadeAmount; 
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ; 
        }  
  lastRun = millis();
    }
}

void cmd_two() {
 // do one LED sequence here
    analogWrite(11, brightness); 
    brightness = brightness + fadeAmount; 
  if (millis() - lastRun > 50) {
      if (brightness == 0 || brightness == 255) {
          fadeAmount = -fadeAmount ; 
        }  
  lastRun = millis();
    }
}



void do_task(int num) {
  switch (num) {
    case 1:
      cmd_one();
      break;
    case 2:
      cmd_two();
      break;
  }
}

void loop() {
 // check for incoming RF data
 if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value==0x8B77887) {
     num = 1  ;
    }
    
  do_task(num);
  
 }
}