Pages: [1] 2 3 4   Go Down
Author Topic: loops  (Read 2836 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how do i create  such a function sy cmd_one()

is it like this void (cmd_one) ?
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 26
Posts: 1148
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// 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 ) ;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i tried sommething, im not sure what that number is for
but still this i tried
en get the error
Code:
#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);   
  };   
}



* error.png (182.08 KB, 1600x900 - viewed 20 times.)
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 26
Posts: 1148
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am sorry but i don't understand
Quote
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...
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?)
Code:
#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;
  }
}
Logged

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

Code:
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".
« Last Edit: April 12, 2012, 05:46: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.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

num = 1;


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

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

Quote
Let's assume the type of "num" is an "int", because the compiler can't.
You need to tell it.
Code:
void do_task(int num)
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.

Pages: [1] 2 3 4   Go Up
Jump to: