RGB LED controlled using IR Remote

The code that you posted is written for the old version (< 3.0) of the IRremote library. What version do you have installed?

Sorry I had reached the limit of 22 messages on my other account
Version 3.3.0 installed for the IRremote library

You should not have created another account. That will be, I think, viewed as a sock puppet and is very much discouraged.

Since you have the latest version of the IRremote library installed you will need to modify your code to work with the new version. The IRremote library github page has instructions.


That is the picture of the circuit

#include "IRremote.h" //include the library
#define Button_1 0xFF30CF
#define Button_2 0xFF18E7
#define Button_3 0xFF7A85
#define Button_4 0xFF10EF
#define Button_5 0xFF38C7
#define Button_6 0xFF5AA5
int receiver = 13; //initialize pin 13 as recevier pin.
uint32_t Previous;
IrReceiver); //create a new instance of receiver
decode_results results;

void setup() {
Serial.begin(9600);
IrReceiver.begin(receiver, ENABLE_LED_FEEDBACK); //start the receiver

pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
if (IrReceiver.decode()) { //if we have received an IR signal
if (IrReceiver.decodedIRData.decodedRawData==0xFFFFFFFF) {
IrReceiver.decodedIRData.decodedRawData=Previous;
}

switch(IrReceiver.decodedIRData.decodedRawData) {
       case Button_1 : digitalWrite(6, HIGH); break;
       case Button_2 : digitalWrite(5, HIGH); break;
       case Button_3 : digitalWrite(3, HIGH); break;
       case Button_4 : digitalWrite(6, LOW); break;
       case Button_5 : digitalWrite(5, LOW); break;
       case Button_6 : digitalWrite(3, LOW); break;
  } 

Serial.println (IrReceiver.decodedIRData.decodedRawData, HEX); //display HEX results
IrReceiver.resume(); //next value
}
Previous=IrReceiver.decodedIRData.decodedRawData;
}

Would this code work in the latest version?

No. It will not.

IrReceiver; //create a new instance of receiver
decode_results results;

Those lines do not belong with the new version.

The code below does work. It is basically your code updated to the new version of the IRremote library with the magic numbers removed and written in my style. You will need to replace the key codes with the ones for your remote. The new version returns key codes differently from the old one. Run this code, press a key and note the value, then replace the value in the code. This code has been successfully tested on my setup (see below). Note that this code is formatted and posted properly in code tags.

// RGB LED control with IR remote.

#include "IRremote.h" //include the library

const byte Button_1 = 1; // red
const byte Button_2 = 2; // green
const byte Button_3 = 3; // blue
const byte Button_4 = 4; // red
const byte Button_5 = 5; // green
const byte Button_6 = 6; // blue

const byte redLedPin = 3;
const byte greenLedPin = 5;
const byte blueLedPin = 6;

const byte IR_RECEIVE_PIN = 13;

void setup()
{
   Serial.begin(115200);

   pinMode(redLedPin, OUTPUT);
   pinMode(greenLedPin, OUTPUT);
   pinMode(blueLedPin, OUTPUT);

   // turn LEDs off
   digitalWrite(blueLedPin, HIGH);
   digitalWrite(greenLedPin , HIGH);
   digitalWrite(redLedPin, HIGH);

   Serial.println("RGB LED control with IR remote");
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}

void loop()
{
   if (IrReceiver.decode())
   {
      unsigned long keycode = IrReceiver.decodedIRData.command;
      Serial.println(keycode, HEX);

      // ignore repeat code
      if ((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT))
      {
         IrReceiver.resume();
         return;
      }

      IrReceiver.resume();
      switch (keycode)  // common cathode LED LOW is lit
      {
         case Button_1 : digitalWrite(redLedPin, LOW); break;
         case Button_2 : digitalWrite(greenLedPin , LOW); break;
         case Button_3 : digitalWrite(blueLedPin, LOW); break;
         case Button_4 : digitalWrite(redLedPin, HIGH); break;
         case Button_5 : digitalWrite(greenLedPin , HIGH); break;
         case Button_6 : digitalWrite(blueLedPin, HIGH); break;
      }
   }
}

With a few changes and the addition of toggling the LED full on and off with the power key.

// RGB LED control with IR remote.

#include "IRremote.h" //include the library

// changed to int data type as that is the data
// type returned by the IRremote library 
const int Button_1 = 1; // red
const int Button_2 = 2; // green
const int Button_3 = 3; // blue
const int Button_4 = 4; // red
const int Button_5 = 5; // green
const int Button_6 = 6; // blue

const byte redLedPin = 3;
const byte greenLedPin = 5;
const byte blueLedPin = 6;

const byte IR_RECEIVE_PIN = 13;

const int powerButton = 23; // decimal value
bool powerState = false;

void setup()
{
   Serial.begin(115200);

   pinMode(redLedPin, OUTPUT);
   pinMode(greenLedPin, OUTPUT);
   pinMode(blueLedPin, OUTPUT);

   // turn LEDs off
   digitalWrite(blueLedPin, HIGH);
   digitalWrite(greenLedPin , HIGH);
   digitalWrite(redLedPin, HIGH);

   Serial.println("RGB LED control with IR remote");
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}

void loop()
{
   if (IrReceiver.decode())
   {
      unsigned long keycode = IrReceiver.decodedIRData.command;
      Serial.println(keycode); // print decimal value

      // ignore repeat code
      if ((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT))
      {
         IrReceiver.resume();
         return;
      }

      IrReceiver.resume();
      switch (keycode)  // common cathode LED LOW is lit
      {
         case Button_1 :
            digitalWrite(redLedPin, LOW);
            break;
         case Button_2 :
            digitalWrite(greenLedPin , LOW);
            break;
         case Button_3 :
            digitalWrite(blueLedPin, LOW);
            break;
         case Button_4 :
            digitalWrite(redLedPin, HIGH);
            break;
         case Button_5 :
            digitalWrite(greenLedPin , HIGH);
            break;
         case Button_6 :
            digitalWrite(blueLedPin, HIGH);
            break;

         // toggle the LED on white (R,G and B lit)
         // and all off with the power button
         case powerButton:
            {
               //Serial.println("power button");
               if (powerState == false)
               {
                  powerState = true;
                  digitalWrite(redLedPin, LOW);
                  digitalWrite(greenLedPin , LOW);
                  digitalWrite(blueLedPin, LOW);
               }
               else
               {
                  powerState = false;
                  digitalWrite(redLedPin, HIGH);
                  digitalWrite(greenLedPin , HIGH);
                  digitalWrite(blueLedPin, HIGH);
               }
            }
            break;

         default:
            Serial.println("invalid key code");
      }
   }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.