Press and Hold IR Remote Control Button

sorry for my poor english

i am working on a ir remote project
i want to do …
1 while remote key 1 pressed green led should on

2 when stop pressing grenn led should off

3 while remote key 2 pressed red led should on

4 when stop pressing red led should off

my code is working fine with only one led but with two led its not . please help me.

my code is

#include <IRLib.h>
int GreenLed = 7;
int RedLed = 10;
int GreenLedState = 0;
int RedLedState = 0;
#define REMOTE_KEY_1 0x20DF8877 //remote key 1
#define REMOTE_KEY_2 0x20DF48B7 //remote key 2

IRrecv My_Receiver(11);//receiver on pin 11
IRdecode My_Decoder; //Decoder object

long GreenLedoffAt = 0;
long RedLedoffAt = 0;

void setup() {
Serial.begin(9600);
My_Receiver.enableIRIn(); // Start the receiver
pinMode(GreenLed, OUTPUT);
digitalWrite(GreenLed, LOW);

pinMode(RedLed, OUTPUT);
digitalWrite(RedLed, LOW);

delay(1000);
}

void loop()
{
// Serial.println(My_Decoder.value, HEX);
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if (My_Decoder.decode_type == NEC) {
Serial.println(My_Decoder.value, HEX);
if (My_Decoder.value == REMOTE_KEY_1) // Yadi remote ka 1 number press kiya hai then
{
digitalWrite(GreenLed, HIGH);
GreenLedoffAt = millis() + 100; // Set the time-out
GreenLedState = 1;
Serial.print(“Green Led on”);
}
else if (My_Decoder.value == REPEAT && GreenLedState == 1)
{
GreenLedoffAt = millis() + 100; // update the time-out
}
}
My_Receiver.resume(); //Restart the receiver
}

// Turn off relay…
if(digitalRead(GreenLed) == HIGH){ //yadi TimerLed is samay on hai aur

if(millis() >= GreenLedoffAt){
digitalWrite(GreenLed, LOW);
GreenLedState = 0;
}
}
//------------------------------------
Serial.println(My_Decoder.value, HEX);
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if (My_Decoder.decode_type == NEC) {
Serial.println(My_Decoder.value, HEX);
if (My_Decoder.value == REMOTE_KEY_2) // Yadi remote ka 2 number press kiya hai then
{
digitalWrite(RedLed, HIGH);
RedLedoffAt = millis() + 100; // Set the time-out
RedLedState = 1;
Serial.print(“Green Led on”);
}
else if (My_Decoder.value == REPEAT && RedLedState == 1)
{
RedLedoffAt = millis() + 100; // update the time-out
}
}
My_Receiver.resume(); //Restart the receiver
}

// Turn off relay…
if(digitalRead(RedLed) == HIGH){ //yadi TimerLed is samay on hai aur

if(millis() >= RedLedoffAt){
digitalWrite(RedLed, LOW);
RedLedState = 0;
}
}

}

if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if (My_Decoder.decode_type == NEC) {
Serial.println(My_Decoder.value, HEX);

^^ That stuff doesn't need to be repeated.
After that, you just need your two if conditions:

if (My_Decoder.value == REMOTE_KEY_1)
{etc}
if (My_Decoder.value == REMOTE_KEY_2)
{etc}

then
My_Receiver.resume();

thank u sir, but i am new for this. i try this but not work can you please edit my code for proper working.
please sir i will be thankful for you.

sir i did edit my code as u say but its not working.
please help me please please please…

now my code is look like this. sorry for my poor english.

#include <IRLib.h>
int GreenLed = 7;
int RedLed = 10;
int GreenLedState = 0;
int RedLedState = 0;
#define REMOTE_KEY_1 0x20DF8877 //remote key 1
#define REMOTE_KEY_2 0x20DF48B7 //remote key 2

IRrecv My_Receiver(11);//receiver on pin 11
IRdecode My_Decoder; //Decoder object

long GreenLedoffAt = 0;
long RedLedoffAt = 0;

void setup() {
Serial.begin(9600);
My_Receiver.enableIRIn(); // Start the receiver
pinMode(GreenLed, OUTPUT);
digitalWrite(GreenLed, LOW);

pinMode(RedLed, OUTPUT);
digitalWrite(RedLed, LOW);

delay(1000);
}

void loop()
{
Serial.println(My_Decoder.value, HEX);
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if (My_Decoder.decode_type == NEC) {
Serial.println(My_Decoder.value, HEX);
if (My_Decoder.value == REMOTE_KEY_1)
{
digitalWrite(GreenLed, HIGH);
GreenLedoffAt = millis() + 100; // Set the time-out
GreenLedState = 1;
Serial.print(“Green Led on”);
}
else if (My_Decoder.value == REPEAT && GreenLedState == 1)
{
GreenLedoffAt = millis() + 100; // update the time-out
}
}
My_Receiver.resume(); //Restart the receiver
}

if(digitalRead(GreenLed) == HIGH){

if(millis() >= GreenLedoffAt){
digitalWrite(GreenLed, LOW);
GreenLedState = 0;

if (My_Decoder.value == REMOTE_KEY_2) // Yadi remote ka 1 number press kiya hai then
{
digitalWrite(RedLed, HIGH);
RedLedoffAt = millis() + 100; // Set the time-out
RedLedState = 1;
Serial.print(“Green Led on”);
}
else if (My_Decoder.value == REPEAT && RedLedState == 1)
{
RedLedoffAt = millis() + 100; // update the time-out
}
}
My_Receiver.resume(); //Restart the receiver
}

// Turn off relay…
if(digitalRead(RedLed) == HIGH){ //yadi TimerLed is samay on hai aur

if(millis() >= RedLedoffAt){
digitalWrite(RedLed, LOW);
RedLedState = 0;
}
}

}

A bit late but, here is a sketch that may give you some help.

//**********************************************************************
 
#include "IRremote.h"
//LarryD

//Sketch to demonstrate using an IR hand remote to control Arduino outputs.
//The remote used here uses the NEC protocol.
//This remote sends the button code (example 0xFF629D) then a repeat code 0xFFFFFFFF
//The repeat code is re-sent as long as an IR remote button is pressed.
//The IR receiver used is the TSOP4838
 
/*  17 IR button remote layout   http://i.imgur.com/X1KIdqI.png
 
   ^
< OK  >  
   v
1  2  3 
4  5  6
7  8  9
*  0  # 
 
*/
 
 
const byte RECV_PIN = 7;   //IR receive pin
 
IRrecv irrecv(RECV_PIN);   //create instance of 'IRrecv'
decode_results results;    //create instance of 'decode_results'
 
unsigned long   TimerUp;   //UP arrow on the remote
boolean         TimerUpFlag = false;
 
unsigned long   TimerDown; //DOWN arrow on the remote
boolean         TimerDownFlag = false;
 
unsigned long TimerIntensity;
unsigned long TimerIncDec;
boolean intesityUpFlag   = false;
boolean intesityDownFlag = false;
int brightness;
 
unsigned long   dummy;
unsigned long * TimerPtr = &dummy; //pointer to the current timer
 
const byte upLED        = 13;  //turns on as long as the UP button is pressed
const byte downLED      = 12;  //turns on as long as the DOWN button is pressed
const byte leftLED      = 11;  //toggles on/off
const byte rightLED     = 10;  //toggles on/off
const byte intensityLED =  9;  //a LED which can have its intensity adjusted
 
 
//                           s e t u p ( )
//**********************************************************************
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); //start receive
 
  pinMode(upLED, OUTPUT);
  pinMode(downLED, OUTPUT);
  pinMode(leftLED, OUTPUT);
  pinMode(rightLED, OUTPUT);
  pinMode(intensityLED, OUTPUT); //this is a PWM output pin
 
} //                    E N D  O F  s e t u p ( )
 
 
 
//                            l o o p ( )
//**********************************************************************
void loop()
{
  if (irrecv.decode(&results)) //is there IR remote button code
  {
    //Serial.println(results.value);
    processButton(); //process button press
    irrecv.resume(); //restart for next button press
  }
 
  //**********************                                //Turn off upLED
  //if timing is enabled, is it time to stop
  if (TimerUpFlag && millis() - TimerUp >= 250ul)
  {
    TimerUpFlag = false; //disable timing
    TimerPtr = &dummy;   //pointer to dummy timer
    digitalWrite(upLED, LOW);
  }
 
  //**********************                                //Turn off downLED
  //if timing is enabled, is it time to stop
  if (TimerDownFlag && millis() - TimerDown >= 250ul)
  {
    TimerDownFlag = false; //disable timing
    TimerPtr = &dummy;     //pointer to dummy timer
    digitalWrite(downLED, LOW);
  }
 
  //**********************                                //LED intensity
  //are we still within the adjustment time
  if (millis() - TimerIntensity <= 300ul)
  {
    //is it time to increase/decrease the intensity
    if (millis() - TimerIncDec >= 200ul)
    {
      TimerIncDec = millis();
 
      if (intesityUpFlag == true) //Increase
      {
        brightness += 5;
        if (brightness > 255)
        {
          brightness = 255;
        }
      }
      else if (intesityDownFlag == true) //Decrease
      {
        brightness -= 5;
        if (brightness < 0)
        {
          brightness = 0;
        }
      }
 
      analogWrite(intensityLED, brightness);
      //Serial.println(brightness); //debug
    }
  }
  //stop increasing/decreasing intensity
  else
  {
    intesityUpFlag = false;
    intesityDownFlag = false;
  }
 
  //************************************
  //Other non blocking code goes here
  //************************************
 
} //                   E N D  O F  l o o p ( )
 
 
 
//======================================================================
//                       F U N C T I O N S
//======================================================================
 
//                   p r o c e s s B u t t o n ( )
//**********************************************************************
//process IR remote button presses
void processButton()
{
  switch (results.value)
  {
    //**********************
    case 0xFF629D:                                           //UP Arrow
      {
        Serial.println("UP");
        TimerPtr = &TimerUp;  //point to this timer
        TimerUpFlag = true;   //enable timing
        digitalWrite(upLED, HIGH);
        TimerUp = millis();
      }
      break;
 
    //**********************
    case 0xFFA857:                                           //DOWN Arrow
      {
        Serial.println("DOWN");
        TimerPtr = &TimerDown;  //point to this timer
        TimerDownFlag = true;   //enable timing
        digitalWrite(downLED, HIGH);
        TimerDown = millis();
      }
      break;
 
    //**********************
    case 0xFF22DD:                                           //LEFT Arrow
      {
        Serial.println("LEFT");
        digitalWrite(leftLED, !digitalRead(leftLED));   //Toggle LED
      }
      break;
 
    //**********************
    case 0xFFC23D:                                           //RIGHT Arrow
      {
        Serial.println("RIGHT");
        digitalWrite(rightLED, !digitalRead(rightLED)); //Toggle LED
      }
      break;
 
    //**********************
    case 0xFF42BD:                                           // * button
      {
        Serial.println("*");
        TimerPtr = &TimerIntensity;  //point to this timer
        intesityUpFlag = true;       //enable intensity up adjustment
        intesityDownFlag = false;
        TimerIncDec = millis();
        TimerIntensity = millis();
      }
      break;
 
    //**********************
   case 0xFF52AD:                                           // # button
      {
        Serial.println("#");
        TimerPtr = &TimerIntensity;  //point to this timer
        intesityDownFlag = true;     //enable intensity down adjustment
        intesityUpFlag = false;
        TimerIncDec = millis();
        TimerIntensity = millis();
      }
      break;
 
    //**********************
    case 0xFF02FD:
      Serial.println("OK");
      break;
 
    //**********************
    case 0xFF6897:
      Serial.println("1");
      break;
 
    //**********************
    case 0xFF9867:
      Serial.println("2");
      break;
 
    //**********************
    case 0xFFB04F:
      Serial.println("3");
      break;
 
    //**********************
    case 0xFF30CF:
      Serial.println("4");
      break;
 
    //**********************
    case 0xFF18E7:
      Serial.println("5");
      break;
 
    //**********************
    case 0xFF7A85:
      Serial.println("6");
      break;
 
    //**********************
    case 0xFF10EF:
      Serial.println("7");
      break;
 
    //**********************
    case 0xFF38C7:
      Serial.println("8");
      break;
 
    //**********************
    case 0xFF5AA5:
      Serial.println("9");
      break;
 
    //**********************
    case 0xFF4AB5:
      Serial.println("0");
      break;
 
    //**********************
    case 0xFFFFFFFF: //Repeat code
      {
        Serial.println("REPEAT");
        *TimerPtr = millis();       //reset the current timer
      }
      break;
 
  } // END switch case
 
} //             E N D  o f  p r o c e s s B u t t o n ( )
 
//**********************************************************************
 
//======================================================================
//                        E N D  O F  C O D E
//======================================================================

you are great Mr. LarryD. thank you very much, thank you very much, thank you very much.
your code is great for me , it is what i looking for. thank you once again.
your code is best for learning ir remote.

sir how can i do same thing with bluetooth module i have HC-05 .
how can i send press and hold command with bluetooth.
can you edit the great code for working with bluetooth module.
please sir if you help me other people will also learn great things.
please help me.