#include <IRremote.hpp>
#define IR_RECEIVE_PIN 2
struct Command {
IRRawDataType code; // the raw IR code
void (*callback)(); // the function to run
};
enum ActiveColor {
GREEN,
RED,
OFF
};
void onPower();
void onIncreaseBrightness();
void onDecreaseBrightness();
void onChangeRed();
void onChangeGreen();
Command commandList[] = {
{0x5DA2FF00, onPower},
{0xFD02FF00, onIncreaseBrightness},
{0x6798FF00, onDecreaseBrightness},
{0x1FE0FF00, onChangeRed},
{0x6F90FF00, onChangeGreen},
};
const int commandCount = sizeof(commandList) / sizeof(commandList[0]);
byte redPin = 12;
byte greenPin = 9;
int brightness = 255;
ActiveColor currentColor = OFF;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
Serial.begin(115520);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}
void loop() {
if (IrReceiver.decode()) {
IRRawDataType incoming = IrReceiver.decodedIRData.decodedRawData;
// Look for matching command
for (int i = 0; i < commandCount; i++) {
if (incoming == commandList[i].code) {
commandList[i].callback();
}
}
IrReceiver.resume();
}
}
void applyBrightness() {
if (currentColor == OFF) {
analogWrite(greenPin, 255); // PWM ON for red mode?
digitalWrite(redPin, HIGH);
}
if (currentColor == RED) {
analogWrite(greenPin, brightness); // PWM ON for red mode?
digitalWrite(redPin, LOW); // LOW = ON (common anode)
}
if (currentColor == GREEN) {
digitalWrite(redPin, HIGH); // HIGH = OFF (common anode)
analogWrite(greenPin, 255 - brightness); // invert PWM
}
}
void onPower() {
if (currentColor == OFF) {
currentColor = GREEN; // turn back on to default color
Serial.println("POWER ON");
} else {
currentColor = OFF;
Serial.println("POWER OFF");
}
applyBrightness();
}
void onIncreaseBrightness() {
Serial.println("INCREASE BRIGHTNESS");
if (currentColor = OFF) {
return;
}
if (brightness < 255)
brightness++;
applyBrightness();
}
void onDecreaseBrightness() {
Serial.println("DECREASE BRIGHTNESS");
if (currentColor == OFF) {
return;
}
if (brightness > 0)
brightness--;
applyBrightness();
}
void onChangeRed() {
Serial.println("Change to Red");
if(currentColor == OFF) {
return;
}
currentColor = RED;
applyBrightness();
}
void onChangeGreen() {
Serial.println("Change to Green");
if (currentColor == OFF) {
return;
}
currentColor = GREEN;
applyBrightness();
}