In this example we will control an RGB led with a remote control, this will involve us connecting an infrared reciever to our Arduino as well.
We will use the buttons 1,2 and 3 to control the Red, Green and blue colours of the RGB LED. You will be able to switch the colours ona nd off which of course means you can mix the colours as well
Here is the remote control we will use in this example
Layout
Arduino pins used
Pin 3 : IR receiver input
Pin 8 : Red cathode
Pin 9 : Green Cathode
Pin 10 : Blue Cathode
I used an IR module and RGB led module as well, the following shows this
Code
The code needs the IRremote remote library to be installed. Then you need to find the codes for the various buttons on the remote control that you wish to use
[codesyntax lang=”cpp”]
#include <IRremote.h>
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume();
}
}
[/codesyntax]
Now upload the code above and open the serial monitor, when you press each button you should see the hex code for the keys you want to use, here is an example
0xFF30CF
0xFF18E7
These are the hex codes for buttons 1 and 2, we will use these in our full example underneath
Here is the complete code example
[codesyntax lang=”cpp”]
#include <IRremote.h> int RECV_PIN = 3; #define redpin 8 #define greenpin 9 #define bluepin 10 boolean state1 = true; boolean state2 = true; boolean state3 = true; IRrecv irrecv(RECV_PIN); decode_results results; void setup() { Serial.begin(9600); pinMode(redpin, OUTPUT); pinMode(greenpin, OUTPUT); pinMode(bluepin, OUTPUT); // Set high for common anode type digitalWrite(redpin, HIGH); digitalWrite(greenpin, HIGH); digitalWrite(bluepin, HIGH); irrecv.enableIRIn(); // Start the receiver } void loop() { if (irrecv.decode(&results)) { switch(results.value) { case 0xFF30CF: Serial.println("1"); if(state1==true) { digitalWrite(redpin, LOW); //red on state1=false; //change the state } else { digitalWrite(redpin, HIGH); //red off state1=true; //change the state } break; case 0xFF18E7: Serial.println("2"); if(state2==true) { digitalWrite(greenpin, LOW); //green on state2=false; } else { digitalWrite(greenpin, HIGH); //green off state2=true; //change the state } break; case 0xFF7A85: Serial.println("3"); if(state3==true) { digitalWrite(bluepin, LOW); //blue on state3=false; } else { digitalWrite(bluepin, HIGH); //blue off state3=true; } break; case 0xFF6897: Serial.println("OFF"); digitalWrite(redpin, HIGH); digitalWrite(greenpin, HIGH); digitalWrite(bluepin, HIGH); state1 = true; state2 = true; state3 = true; break; default : Serial.println("Invalid Option"); break; } delay(250); irrecv.resume(); } }
[/codesyntax]
Links
KEYES Infrared receiver sensor module for arduino
5PCS/LOT RGB 3 Color SMD Full Color LED Module For Arduino