#include <IRremote.hpp>
const byte irReceivePin = 2;
const byte rPin = 9;
const byte gPin = 6;
const byte bPin = 5;
byte brightness = 255;
byte r, g, b;
byte outR, outG, outB;
void adjustedRgbForBrightness(uint8_t r, uint8_t g, uint8_t b, uint8_t targetBrightness,
uint8_t &outR, uint8_t &outG, uint8_t &outB)
{
float currentBrightness = 0.299f * r + 0.587f * g + 0.114f * b;
if (currentBrightness == 0) {
outR = outG = outB = 0;
return;
}
float scale = targetBrightness / currentBrightness;
int tempR = r * scale;
int tempG = g * scale;
int tempB = b * scale;
outR = tempR > 255 ? 255 : tempR;
outG = tempG > 255 ? 255 : tempG;
outB = tempB > 255 ? 255 : tempB;
}
void handleCommand() {
if (IrReceiver.decode()) {
switch (IrReceiver.decodedIRData.decodedRawData) {
case 0x5DA2FF00 : // POWER for full bright white
r = 255; g = 255; b = 255; brightness = 255;
break;
case 0x3DC2FF00 : // undo for all lights off
r = 0; g = 0; b = 0; brightness = 0;
break;
case 0xCF30FF00 : // 1 for red, the current brightness needs to be used
r = 255; g = 0; b = 0;
break;
case 0xE718FF00: // 2 for green the current brightness needs to be used
r = 0; g = 255; b = 0;
break;
case 0x857AFF00: // 3 for blue , the current brightness needs to be used
r = 0; g = 0; b = 255;
break;
case 0xEF10FF00: // 4 for cyan, the current brightness needs to be used
r = 92, g = 255, b = 255;
break;
case 0xC738FF00: // 5 for yellow, the current brightness needs to be used
r = 255, g = 255, b = 0;
break;
case 0xA55AFF00: // 6 for white, the current brightness needs to be used
r = 255; g = 255; b = 255;
break;
case 0X1FE0FF00: // the brightness decrease
brightness--;
break;
case 0x6F90FF00: // the brightness increases
brightness++;
break;
default: break;
}
adjustedRgbForBrightness(r, g, b, brightness, outR, outG, outB);
analogWrite(rPin, outR);
analogWrite(gPin, outG);
analogWrite(bPin, outB);
IrReceiver.resume();
}
}
void setup() {
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
Serial.begin(115200);
IrReceiver.begin(irReceivePin);
Serial.println(F("Ready"));
}
void loop() {
handleCommand();
}