Pages: 1 2 [3] 4   Go Down
Author Topic: loops  (Read 3534 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

i opened that and i pushed the butten on my remote "1" and a second time the FFFFFFF in between was because i pushed them to fast
Quote
8B73AC5
FFFFFFFF
8B73AC5
Logged

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

Well yeah, your cmd_one() and cmd_two() are identical, so regardless of which button you push, it's doing the same thing.  So change one of them to do something else.  Right now you have both of them fading an LED up and down again.

Also, you need to understand that as long as 'num' does not change, the LED will continue to fade on and off indefinitely.  That's how you have it coded (which was the example I gave you too.)  So basically what happens is this:
You push a button, and it registers as either num = 1 or num = 2.  At that point the LED will start to fade on and off.  If you don't do anything after that, it will continue to fade on and off.  It's not till you push another button that causes 'num' to change value, will it do something else.

If that's not the desired result, then you need to explain more about what you want them to do.  From the beginning, from the moment you push a button.
Logged

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

Change your current cmd_one(0 and cmd_two() functions for these two, see what happens:
Code:
void cmd_one() {
  // langzaam
  analogWrite(9, brightness);
  brightness = brightness + fadeAmount;
  if (millis() - lastRun > 150) {
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    } 
    lastRun = millis();
  }
}

void cmd_two() {
  // vlug
  analogWrite(9, brightness);
  brightness = brightness + fadeAmount;
  if (millis() - lastRun > 25) {
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    } 
    lastRun = millis();
  }
}
What happens now?
Logged

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

if i push button "1" once the led on pin 9 lights up in his lowest setting and does notthing. he does't become brighter.
but when i push an other button of my remote (or even a button of a romote i never used) the led goes a bit brighter (only a little bit.)
so i think there ar 2 problems: he does resume to check if there are signals (otherwise the wouldn't happen anything, i think) but hie doesn't cair about the if statment in the
void loop. and he doesn't repeat cmd_one() or cmd_two()


you also said
Quote
You push a button, and it registers as either num = 1 or num = 2.  At that point the LED will start to fade on and off.  If you don't do anything after that, it will continue to fade on and off.  It's not till you push another button that causes 'num' to change value, will it do something else.

If that's not the desired result, then you need to explain more about what you want them to do.  From the beginning, from the moment you push a button.
what i try to create is a programme for a rgb ledstrip that can dim a color independently (in this i've more or less succeded, this i think i can handle), but what i don't know is how to do the following: push button 1, collors begin to fade (in a certain order, not random), push button 2 (for example) a trafic light, and so on, but al these go on if ther is no other command recieved, for example with  button 1: red fades on, then green fades on then red fades off and blue fades on , green fades off , red fades on and blue fades off and this should be repeated unless these is an other command.
 
i am using here only pin 9 to make every thing less dificult, so i think num has to remain 1 (for ever) Unless it gets an outher command that sais num hes to be an other value
sorry that is repeat myself so muth but does it make sence?
« Last Edit: April 13, 2012, 05:59:39 am by timothysg1 » Logged

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

Try this:

Connect one LED on pin 9, and another on pin 10.  Then use these two functions:
Code:
void cmd_one() {
  // langzaam
  if (millis() - lastRun > 150) {
    analogWrite(9, brightness);
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }  
    lastRun = millis();
  }
}

void cmd_two() {
  // vlug
  if (millis() - lastRun > 25) {
    analogWrite(10, brightness);
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }  
    lastRun = millis();
  }
}

And your loop has an error in it, you put do_task() inside of the if statement, it needs to be outside of it so it's constantly running.  Here's a revised version:
Code:
void loop() {
  // check for incoming RF data
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value==0x8B73AC5) {
      num = 1  ;
    }
    if (results.value==0x8B77887) {
      num = 2 ;
    }
    irrecv.resume();
  }
  do_task(num);
}

Once you have that in place, with the two LEDs, pressing button 1 should make the LED on pin 9 fade on and off constantly.  Pressing button 2 should switch to the LED on pin 10.  Now, you will notice that when you press a different button, it instantly stops whatever it was doing with the previous LED, so you may end up with one LED turned on, while the other is fading on and off.  Don't worry about it for now, this is just a test, see if you can even get the individual buttont on your remote to actually address two different LEDs.
Logged

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

thats exactly what happens, i tried some other buttons on my remote to see if there is an unexpected reaction, and ther isn't any, so far so good.
What 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

Well, the rest is up to you.  I gave you the building blocks for driving two separate LEDs on two separate pins, using two separate buttons on your remote.  You can fold the actions back to a single pin in which case you will only be controlling one LED, but letting it do two different things ( cmd_one() or cmd_two() ).  Either way, the sky is the limit at this point.  There are many way to do what you're trying to accomplish, you just have to keep playing, testing, but most of all, have fun with it.
Logged

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

one last thing, the line
Code:
lastRun = millis();
does it does sommething, is that what the delay resambles, or is it only because it's neccesary for the code?
Logged

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

You're using 'lastRun' to check the elapsed time.  Remember this line:
Code:
  if (millis() - lastRun > 150) {
That is where it gets used.  You're taking the current elapsed time from millis(), subtracting lastRun and seeing if the result is bigger than '150'.  So 'lastRun' must be set each time a loop runs.  Otherwise it will never reset.
Logged

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

i dont understaand why it doesn"t FULLY execute cmd_one
Code:
#include "IRremote.h"
#include "IRremoteInt.h"


int num;


long lastRun;
int RECV_PIN = 0;     // pin0  =linker poot reciever, middenste poot reciever= 5V, rechter poot reciever =grd  velleman:IR38DM       
IRrecv irrecv(RECV_PIN);
decode_results results;


int brightnessR = 0;    // how bright the RED LED is
int fadeAmountR = 5;

int brightnessG = 0;    // how bright the GREEN LED is
int fadeAmountG = 5;

int brightnessB = 0;    // how bright the BLUE LED is
int fadeAmountB = 5;




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



void cmd_one() {
  // langzaam
  if (millis() - lastRun > 150) {
    analogWrite(9, brightnessR);
                                                                                  analogWrite(10, brightnessG);
                                                                                  analogWrite(11, brightnessB);
                                                                                 
                                                                                   brightnessR = brightnessR + fadeAmountR;     // red fades on (works fine)
   
                                                                                  if (brightnessR == 255 && brightnessB == 0) {                                                                             
                                                                                    fadeAmountR = -fadeAmountR ;                   // red fades off (works fine)
                                                                                    brightnessG = brightnessG + fadeAmountG;      // green fades on (goes on a bit but not completely)
                                                                                   }
                                                                                  if (brightnessG == 255 && brightnessR == 0) {
                                                                                   fadeAmountG = -fadeAmountG ;                  //green fades on (doesn't work)
                                                                                   brightnessB = brightnessB + fadeAmountB;      // Blue fades one(doesn't work)
                                                                                  }
                                                                                 if(brightnessB == 255 && brightnessG == 0){
                                                                                     fadeAmountB = -fadeAmountB;                 // blue fades off(doesn't work)
                                                                                   }
    lastRun = millis();
  }
}

void cmd_two() {
  // vlug
  if (millis() - lastRun > 25) {
    analogWrite(10, brightnessG);
    brightnessG = brightnessG + fadeAmountG;
    if (brightnessG == 0 || brightnessG == 255) {
      fadeAmountG = -fadeAmountG ;
    } 
    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==0x8B73AC5) {
      num = 1  ;
    }
    if (results.value==0x8B77887) {
      num = 2 ;
    }
    irrecv.resume();
  }
  do_task(num);
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19020
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void cmd_one() {
  // langzaam
  if (millis() - lastRun > 150) {
    analogWrite(9, brightnessR);
                                                                                  analogWrite(10, brightnessG);
                                                                                  analogWrite(11, brightnessB);
                                                                                 
                                                                                   brightnessR = brightnessR + fadeAmountR;     // red fades on (works fine)
   
                                                                                  if (brightnessR == 255 && brightnessB == 0) {                                                                             
                 

Get rid of the tab characters and use spaces.
Logged


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

hier you go
Code:
#include "IRremote.h"
#include "IRremoteInt.h"


int num;


long lastRun;
int RECV_PIN = 0;     // pin0  =linker poot reciever, middenste poot reciever= 5V, rechter poot reciever =grd  velleman:IR38DM       
IRrecv irrecv(RECV_PIN);
decode_results results;


int brightnessR = 0;    // how bright the RED LED is
int fadeAmountR = 5;

int brightnessG = 0;    // how bright the GREEN LED is
int fadeAmountG = 5;

int brightnessB = 0;    // how bright the BLUE LED is
int fadeAmountB = 5;




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



void cmd_one() {
  // langzaam
  if (millis() - lastRun > 150) {
    analogWrite(9, brightnessR);
    analogWrite(10, brightnessG);
    analogWrite(11, brightnessB);
                                                                                 
     brightnessR = brightnessR + fadeAmountR;     // red fades on (works fine)
   
      if (brightnessR == 255 && brightnessB == 0) {                                                                             
       fadeAmountR = -fadeAmountR ;                   // red fades off (works fine)
        brightnessG = brightnessG + fadeAmountG;      // green fades on (goes on a bit but not completely)
         }

         if (brightnessG == 255 && brightnessR == 0) {
            fadeAmountG = -fadeAmountG ;                  //green fades on (doesn't work)
               brightnessB = brightnessB + fadeAmountB;      // Blue fades one(doesn't work)
              }
          if(brightnessB == 255 && brightnessG == 0){
            fadeAmountB = -fadeAmountB;                 // blue fades off(doesn't work)
                }
    lastRun = millis();
  }
}

void cmd_two() {
  // vlug
  if (millis() - lastRun > 25) {
    analogWrite(10, brightnessG);
    brightnessG = brightnessG + fadeAmountG;
    if (brightnessG == 0 || brightnessG == 255) {
      fadeAmountG = -fadeAmountG ;
    } 
    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==0x8B73AC5) {
      num = 1  ;
    }
    if (results.value==0x8B77887) {
      num = 2 ;
    }
    irrecv.resume();
  }
  do_task(num);
}
 
 
 
« Last Edit: April 20, 2012, 05:15:01 am by timothysg1 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19020
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't see anything obvious, but I suggest you put some serial prints in to show what is happening. With labels.

eg.

"cmd_two".

"brightnessG was 5".

"brightnessG is now 10".
Logged


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

is there a way to let the arduino software print those?
otherwise i do it on pure observation
Logged

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

Code:
  if (brightnessR == 255 && brightnessB == 0) {                                                                             
    fadeAmountR = -fadeAmountR ;                   // red fades off (works fine)
    brightnessG = brightnessG + fadeAmountG;      // green fades on (goes on a bit but not completely)
  }
This test TRUE only once.  So brightnessG is incremented only once.  Think about it, you're testing when brightnessR == 255 and when that's TRUE, you flip the value of fadeAmountR.  At the next cycle, brightnessR is no longer 255, so it will never enter that loop again.  BrightnessG never gets incremented after that.
Logged

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