IR reciever and LED

I am working with an IR receiver, and would like to switch a LED on and off.
When I press the button the LED should turn on.
When I release the button the LED should turn off.

Here is the code for turning on the LED.

#include <IRremote.h>

int RECV_PIN = 2;  // IR Reciever
int LED_PIN = 6;  // LED

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup(){
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED_PIN, OUTPUT);  
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 3672802284){
      digitalWrite(LED_PIN, HIGH);
    }
    irrecv.resume(); // Receive the next value
  }
}

No problem turning on the LED.
How do I turn the LED of, when I release the button?

If your remote control sends continuous pulses while you hold the button down, then what you need to do is turn the LED off if you haven't received any pulses for a certain amount of time. If your remote only sends one pulse, then you can't detect when the button is released.

My remote does send continuous pulses.

Something like this?

#include <IRremote.h>

int RECV_PIN = 2;  // IR Reciever
int LED_PIN = 6;  // LED

IRrecv irrecv(RECV_PIN);

decode_results results;

int LastRecieved = 0;

void setup(){
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED_PIN, OUTPUT);  
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 3672802284){
      digitalWrite(LED_PIN, HIGH);
    }
    LastRecieved = millis();
    irrecv.resume(); // Receive the next value
  }

  if ((LastRecieved + 10) < millis()){
    digitalWrite(LED_PIN, LOW);
  }

}

Now the LED flickers when I press the button.

That's almost right:

  1. lastReceived should have type unsigned long, not int;

  2. The line "LastRecieved = millis();" should probably be inside the inner if-statement;

  3. The test should be "if (millis() - LastReceived > 10)" to allow for wrapround;

  4. I suspect that your remote is sending pulses at more like 100ms intervals, so 10ms is too short a timeout.

Thanks. That worked.
This I the code I ended up with.

#include <IRremote.h>

int RECV_PIN = 2;  // IR Reciever
int LED_PIN = 6;  // LED

IRrecv irrecv(RECV_PIN);

decode_results results;

long LastRecieved = 0;

void setup(){
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED_PIN, OUTPUT);  
  Serial.begin(9600);
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 3672802284){
      digitalWrite(LED_PIN, HIGH);
      Serial.println(millis() - LastRecieved);
      LastRecieved = millis();
    }
    irrecv.resume(); // Receive the next value
  }
  if (millis()- LastRecieved > 120) {
    digitalWrite(LED_PIN, LOW);
  }
}

I used this code to measure the time for each transmitted puls from the remote.
Serial.println(millis() - LastRecieved);
107-108 ms

btw in case it's of any use to you, I have a PCB design (and spare PCBs) for a 1-button remote control, based on the ATtiny45. I built it to turn my TV, PVR and audio systems on or off in one go.

Its for a preamp with a motorized pot for the volume.

My plan is to make a 6 button remote based on the PIC16F684.
4 input, volume up, volume down.

Thanks anyway.