Go Down

Topic: loops (Read 3 times) previous topic - next topic

timothysg1

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

KirAsh4

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.

KirAsh4

Change your current cmd_one(0 and cmd_two() functions for these two, see what happens:
Code: [Select]

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?

timothysg1

#33
Apr 13, 2012, 12:41 pm Last Edit: Apr 13, 2012, 12:59 pm by timothysg1 Reason: 1
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?

KirAsh4

Try this:

Connect one LED on pin 9, and another on pin 10.  Then use these two functions:
Code: [Select]

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

Go Up