fade led using IR remote

Hi
i want to make a simple application: turn on/off a fading led using IR remote. I am using IRremote library (A Multi-Protocol Infrared Remote Library for the Arduino
And my code just doesnt work.. It turns on when i first press the button, but doesnt react on next signals. Just dont know what to do..
here is the main part

void loop()  
{ 
  if (irrecv.decode(&results))
  {
   
  if (results.value==52160) {  //it is the value remote button i use
    if(sw==1) sw=0;
    else sw=1;
     irrecv.resume();
  }
}
 if(sw==1) fade();  //fade led
if(sw==0) zero();  // turn led off
}

Hey, check and see if your codes are "multicode" or not, if so then the first press and second press will send alternate codes!

Probably best if you post all your code. That said, consider putting some serial.prints in there so you can see what it is doing.

Okay, actually the task is a little more complicated, i want ot turn on/off RGB lamp which is changing colours in random sequence
when i press "on" button it starts changing colours, but doesn't turn off when i press the button again

#include <IRremote.h>   
#include <Metro.h>
const int irReceivePin =2;
Metro ledMetro = Metro(10) ; 
Metro irmetro = Metro(500);
IRrecv irrecv(irReceivePin); 
decode_results results;   
int prevKey, prevKey2;
float RGB1[3]; 
float RGB2[3]; 
float INC[3]; 
int sw;
int fadeAmount = 5;
 int brightness = 0;
int red, green, blue; 
 
int RedPin = 3; 
int GreenPin = 5; 
int BluePin = 6; 
 
void setup()  
{  Serial.begin(9600);
  pinMode(irReceivePin, INPUT);
  irrecv.enableIRIn(); 
        randomSeed(analogRead(0)); 
   
        RGB1[0] = 0; 
        RGB1[1] = 0; 
        RGB1[2] = 0; 
   
        RGB2[0] = random(256); 
        RGB2[1] = random(256); 
        RGB2[2] = random(256);   

}  
  
void loop()  
{  
      
   if(sw==1) lamp();
if(sw==0) zero();

  if (irrecv.decode(&results))
  {Serial.println(results.value);
   
  if (results.value==52160) {
    if(sw==1) sw=0;
    else sw=1;
   
     irrecv.resume();
  }
  
 }
}


void lamp() {
   randomSeed(analogRead(0)); 
   
        for (int x=0; x<3; x++) { 
                INC[x] = (RGB1[x] - RGB2[x]) / 256; }  
         for (int x=0; x<256; x++) { 
            if (ledMetro.check() == 1) {
           
          
                red = int(RGB1[0]); 
                green = int(RGB1[1]); 
                blue = int(RGB1[2]); 
 
                analogWrite (RedPin, red);   
                analogWrite (GreenPin, green);    
                analogWrite (BluePin, blue);      
           
                RGB1[0] -= INC[0]; 
                RGB1[1] -= INC[1];     
                RGB1[2] -= INC[2];     
           
         }}

label:
        for (int x=0; x<3; x++) { 
         
                RGB2[x] = random(556)-300; 
                RGB2[x] = constrain(RGB2[x], 0, 255);
              
           
        }

if(RGB2[0]==0 && RGB2[1]==0 && RGB2[2]==0) goto label;
}

void zero() {
   analogWrite (RedPin, 0);   
                analogWrite (GreenPin, 0);    
                analogWrite (BluePin, 0);   
}

Println results: first line is number of ir button, and then always "0", and nothing when i push the button

OK I may be wrong here but are you using a digital pin (2) to recieve the IR , also how exactly have you done the hardware, as far as im aware IR codes are HEX data so they must use a serial input, are you revieving using a IR photo diode, if so im sure that the digital in pin will not suffice for this application.

Are you able to get better results with the examples/IRrecvDemo sketch? According to the link you provided, you may need to tweak the library to accommodate the protocol for your particular remote device.

Also, I didn't dig through lamp() in any detail, but you can certainly replace that goto with a do while.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292326335

Oops yeah just actually read the link, so yeah its for a digital string, anyway still would use the com prt for serial, anyway check out, http://www.remotecentral.com/ for some actuall raw binarys of the codes!!

Println results: first line is number of ir button, and then always "0", and nothing when i push the button

Add a Serial.print() statement to show when lamp() is entered, and when it ends. There are a lot of hidden delays going on there. When lamp() is executing, you are not reading IR data.

Thank you all very much, hope I will find the solution soon

Is it possible to make program read IR data and run a lamp() program simultaneously? I want to be able to turn the fading lamp off when the fading program is running?
What is the trick?

What is the trick?

Not using delay(). Or timers of the sort you are using.

#include <boarddefs.h>
#include <IRremote.h>
const int receiver = 3; // pin 1 of IR receiver to Arduino digital pin 11
const int ledPin = 9;
int Led = 12;
int fadeValue;
int lastCounter = 1;
int counter;

IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;

void setup()

{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
irrecv.enableIRIn();
}

void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
}
counter = lastCounter;
if (irrecv.decode(&results))
{
if (results.value == 0x202A857)
{
counter ++;
}
if (results.value == 0x20238C7)
{
counter --;
}
irrecv.resume();
}

if (counter == 5)
{
digitalWrite( Led , HIGH);
delay(500);
digitalWrite( Led , LOW);
delay(100);
}
if (counter > 5){ //maximum for counter = 5
counter = 1;
}

if (counter < 2){ //minimum for counter = 1
counter = 1;
}

switch (counter){ //depending on the counter the fadevalue is sent to the led

case 1:
fadeValue = 00;
break;

case 2:
fadeValue = 50;
break;

case 3:
fadeValue = 120;
break;

case 4:
fadeValue = 185;
break;

case 5:
fadeValue = 255;
break;

}

analogWrite(ledPin , fadeValue); //set led with PWM value
lastCounter=counter;

}

Did you have a question to go with that code you still haven't learned to post properly? Fix your last post before asking the question.