#include <IRremote.h>
int recvPin = 8; // IR Receiver - Arduino Pin Number 8
int redPin = 6; // RED Output Pin
int greenPin = 5; // GREEN Output Pin
int bluePin = 3; // BLUE Output Pin
int relay1 = 22;
int relay2 = 23;
int relay3 = 24;
int relay4 = 25;
int relay5 = 26;
int relay6 = 27;
int relay7 = 28;
int relay8 = 29;
int relayState[] = {0,0,0,0,0,0,0,0,0,0,0};
int intensity = 10; // Intensity variable
int speedValue = 5; // Speed Variable
int currentColors[] = {0, 0, 0}; // Current Color outputed variable (black by default)
bool customLoop = false; // Variable telling us we are in a custom animation loop
unsigned long previousMillis = 0; // variable for the delay function
IRrecv irrecv(recvPin);
decode_results results;
#define code1 3125149440
#define code2 3108437760
#define code3 3091726080
#define code4 3141861120
#define code5 3208707840
#define code6 3158572800
#define code7 4161273600
#define code8 3927310080
// Defining codes for the remote
#define ON_CODE 16236607
#define OFF_CODE 16203967
#define INTENSITY_UP_CODE 16187647
#define INTENSITY_DN_CODE 16220287
#define RED_CODE 16195807
#define GREEN_CODE 16228447
#define BLUE_CODE 16212127
#define WHITE_CODE 16244767
#define ORANGE_CODE 16191727
#define TURQUOISE_CODE 16232527
#define NAVY_CODE 16208047
#define BROWN_CODE 16199887
#define TEAL_CODE 0xFF28D17
#define PURPLE_DARK_CODE 16216207
#define ORANGE_LIGHT_CODE 16189687
#define BLUE_LIGHT_CODE 16222327
#define PINK_DARK_CODE 16206007
#define YELLOW_CODE 16197847
#define BLUE_BABY_CODE 16230487
#define PINK_CODE 16214167
#define FLASH_CODE 16240687
#define STROBE_CODE 16248847
#define FADE_CODE 16238647
#define SMOOTH_CODE 16246807
// defining the avaialble colors one by one
int BLACK_COLOR[3] = {0, 0, 0};
int RED_COLOR[3] = {255, 0, 0};
int GREEN_COLOR[3] = {0, 255, 0};
int BLUE_COLOR[3] = {0, 0, 255};
int WHITE_COLOR[3] = {255, 255, 255};
int ORANGE_COLOR[3] = {255, 128, 0};
int TURQUOISE_COLOR[3] = {0, 255, 128};
int NAVY_COLOR[3] = {0, 76, 153};
int BROWN_COLOR[3] = {153, 76, 0};
int TEAL_COLOR[3] = {0, 102, 102};
int PURPLE_DARK_COLOR[3] = {102, 0, 51};
int ORANGE_LIGHT_COLOR[3] = {255, 153, 51};
int BLUE_LIGHT_COLOR[3] = {0, 255, 255};
int PINK_DARK_COLOR[3] = {255, 0, 128};
int YELLOW_COLOR[3] = {255, 255, 0};
int BLUE_BABY_COLOR[3] = {51, 153, 255};
int PINK_COLOR[3] = {255, 102, 178};
// defining all the available remote codes in an array
int AVAILABLE_CODES[32] = {ON_CODE, OFF_CODE, INTENSITY_UP_CODE, INTENSITY_DN_CODE, RED_CODE, GREEN_CODE, BLUE_CODE, WHITE_CODE, ORANGE_CODE, TURQUOISE_CODE, NAVY_CODE, BROWN_CODE, TEAL_CODE, PURPLE_DARK_CODE, ORANGE_LIGHT_CODE, BLUE_LIGHT_CODE, PINK_DARK_CODE, YELLOW_CODE, BLUE_BABY_CODE, PINK_CODE, FLASH_CODE, FADE_CODE, SMOOTH_CODE, STROBE_CODE, code1, code2, code3, code4, code5, code6, code7, code8};
// defining all the available colors in an array
int AVAILABLE_COLORS[16][3] = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}, {255, 128, 0}, {0, 255, 128}, {0, 76, 153}, {153, 76, 0}, {0, 102, 102}, {102, 0, 51}, {255, 153, 51}, {0, 255, 255}, {255, 0, 127}, {255, 255, 0}, {51, 153, 255}, {255, 102, 158}} ;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);
}
// function for interpreting the incoming code and eighter setting a fixed color or starting a custom loop function
void interpretRemoteCode(int code) {
int randomColor[3] = {random(256), random(256), random(256)};
switch (code) {
case ON_CODE: setColor(randomColor); break;
case OFF_CODE: setColor(BLACK_COLOR); break;
case INTENSITY_UP_CODE: raiseIntensity(); break;
case INTENSITY_DN_CODE: lowerIntensity(); break;
case RED_CODE: setColor(RED_COLOR); break;
case GREEN_CODE: setColor(GREEN_COLOR); break;
case BLUE_CODE: setColor(BLUE_COLOR); break;
case WHITE_CODE: setColor(WHITE_COLOR); break;
case ORANGE_CODE: setColor(ORANGE_COLOR); break;
case TURQUOISE_CODE: setColor(TURQUOISE_COLOR); break;
case NAVY_CODE: setColor(NAVY_COLOR); break;
case BROWN_CODE: setColor(BROWN_COLOR); break;
case TEAL_CODE: setColor(TEAL_COLOR); break;
case PURPLE_DARK_CODE: setColor(PURPLE_DARK_COLOR); break;
case ORANGE_LIGHT_CODE: setColor(ORANGE_LIGHT_COLOR); break;
case BLUE_LIGHT_CODE: setColor(BLUE_LIGHT_COLOR); break;
case PINK_DARK_CODE: setColor(PINK_DARK_COLOR); break;
case YELLOW_CODE: setColor(YELLOW_COLOR); break;
case BLUE_BABY_CODE: setColor(BLUE_BABY_COLOR); break;
case PINK_CODE: setColor(PINK_COLOR); break;
case FLASH_CODE: flash(); break;
case STROBE_CODE: strobe(); break;
case FADE_CODE: fade(); break;
case SMOOTH_CODE: crossFade(); break;
case code1:
if(relayState[5] == 0) {
digitalWrite(relay1, HIGH);
relayState[5] = 1;
} else {
digitalWrite(relay1, LOW);
relayState[5] = 0;
}
break;
case code2:
if(relayState[6] == 0) {
digitalWrite(relay2, HIGH);
relayState[6] = 1;
} else {
digitalWrite(relay2, LOW);
relayState[6] = 0;
}
break;
case code3:
if(relayState[7] == 0) {
digitalWrite(relay3, HIGH);
relayState[7] = 1;
} else {
digitalWrite(relay3, LOW);
relayState[7] = 0;
}
break;
case code4:
if(relayState[8] == 0) {
digitalWrite(relay4, HIGH);
relayState[8] = 1;
} else {
digitalWrite(relay4, LOW);
relayState[8] = 0;
}
break;
case code5:
if(relayState[9] == 0) {
digitalWrite(relay5, HIGH);
relayState[9] = 1;
} else {
digitalWrite(relay5, LOW);
relayState[9] = 0;
}
break;
case code6:
if(relayState[10] == 0) {
digitalWrite(relay6, HIGH);
relayState[10] = 1;
} else {
digitalWrite(relay6, LOW);
relayState[10] = 0;
}
break;
case code7:
if(relayState[12] == 0) {
digitalWrite(relay7, HIGH);
relayState[12] = 1;
} else {
digitalWrite(relay7, LOW);
relayState[12] = 0;
}
break;
case code8:
if(relayState[13] == 0) {
digitalWrite(relay8, HIGH);
relayState[13] = 1;
} else {
digitalWrite(relay8, LOW);
relayState[13] = 0;
}
break;
}
}
// raise the intensity of light or the speed of animation
void raiseIntensity() {
if (!customLoop) {
if (intensity <= 9) {
intensity++;
}
} else if (speedValue <= 9) {
speedValue++;
}
}
// lower the intensity of light or the speed of animation
void lowerIntensity() {
if (!customLoop) {
if (intensity >= 2) {
intensity--;
}
} else if (speedValue >= 2) {
speedValue--;
}
}
// helper function to check if a value is present in an array
int existsInArray(int compareValue, int arrayName[], int arraySize) {
for (int x = 0; x < arraySize; x = x + 1) {
if (arrayName[x] == compareValue) {
return true;
}
}
return false;
}
// set the currentColors variable
void setColor(int colors[]) {
currentColors[0] = colors[0];
currentColors[1] = colors[1];
currentColors[2] = colors[2];
}
// calculate the intensity and send the current color out via the output pins
void sendColor()
{
if (customLoop == false) {
int red = currentColors[0];
int green = currentColors[1];
int blue = currentColors[2];
int mappedIntensity = map(intensity, 0, 10, 0, 255);
int redComputed = map(red, 0, 255, 0, mappedIntensity);
int blueComputed = map(blue, 0, 255, 0, mappedIntensity);
int greenComputed = map(green, 0, 255, 0, mappedIntensity);
analogWrite(redPin, redComputed); // Sends PWM signal to the Red pin
analogWrite(greenPin, greenComputed);
analogWrite(bluePin, blueComputed);
}
}
// check for a code from the remote every 100 milliseconds
void loop() {
if (IrReceiver.decode())
{
Serial.println(IrReceiver.decodedIRData.decodedRawData);
IrReceiver.resume();
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 100) {
previousMillis = currentMillis;
findCode();
}
}
// decode remote controll code and if found in the array of available codes interpret it
void findCode() {
if (irrecv.decode(&results)) {
if (existsInArray(results.value, AVAILABLE_CODES, 32)) {
Serial.print (results.value);
Serial.println("Code Found");
interpretRemoteCode(results.value);
if (results.value != FLASH_CODE && results.value != STROBE_CODE && results.value != FADE_CODE && results.value != SMOOTH_CODE) {
customLoop = false;
sendColor();
}
} else {
Serial.println("Invalid Code");
}
irrecv.resume();
}
}
// custom flashing function
void flash() {
customLoop = true; // set the variable so that the program knows we are in a custom animation loop
unsigned long previousMillis = 0;
while (customLoop) { // while we are still in the custom animation loop
if (irrecv.decode(&results)) { // check for incomming ir code and if found exit the loop and interpret the code
if (existsInArray(results.value, AVAILABLE_CODES, 32) && results.value != INTENSITY_UP_CODE && results.value != INTENSITY_DN_CODE && results.value != FLASH_CODE) {
Serial.println("return break");
return; break;
interpretRemoteCode(results.value);
} else if (results.value == INTENSITY_UP_CODE) { // if the code is to raise or lower the speed
raiseIntensity(); // raise the speed
} else if (results.value == INTENSITY_DN_CODE) {
lowerIntensity(); // or lower the speed
}
irrecv.resume();
}
int randomNumber = random(16); // get a random number from 0 to 16
// set temporary variales for the red, green and blue values
int red = AVAILABLE_COLORS[randomNumber][0];
int green = AVAILABLE_COLORS[randomNumber][1];
int blue = AVAILABLE_COLORS[randomNumber][2];
unsigned long currentMillis = millis();
// set a X animation delay from 200 to 3000 milliseconds based on the speed variable
int mappedSpeed = map(speedValue, 10, 1, 200, 3000);
// every X milliseconds
if (currentMillis - previousMillis >= mappedSpeed) {
previousMillis = currentMillis;
// send the color variables through the digital ouput pins
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
}
}
void strobe() {
customLoop = true;
unsigned long previousMillis = 0;
int i = 1;
while (customLoop) {
if (irrecv.decode(&results)) {
if (existsInArray(results.value, AVAILABLE_CODES, 32) && results.value != INTENSITY_UP_CODE && results.value != INTENSITY_DN_CODE && results.value != STROBE_CODE) {
Serial.println("return break");
return; break;
interpretRemoteCode(results.value);
} else if (results.value == INTENSITY_UP_CODE) {
raiseIntensity();
} else if (results.value == INTENSITY_DN_CODE) {
lowerIntensity();
}
irrecv.resume();
}
int randomNumber = random(16);
int red = AVAILABLE_COLORS[randomNumber][0];
int green = AVAILABLE_COLORS[randomNumber][1];
int blue = AVAILABLE_COLORS[randomNumber][2];
unsigned long currentMillis = millis();
int mappedSpeed = map(speedValue, 10, 1, 50, 1500);
if (currentMillis - previousMillis >= mappedSpeed) {
previousMillis = currentMillis;
if ( (i % 2) == 0) {
analogWrite(redPin, red); // Sends PWM signal to the Red pin
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
} else {
analogWrite(redPin, 0); // Sends PWM signal to the Red pin
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
i++;
}
}
}
void fade() {
customLoop = true;
unsigned long previousMillis = 0;
int i = 1;
int randomNumber = random(16);
int red = AVAILABLE_COLORS[randomNumber][0];
int green = AVAILABLE_COLORS[randomNumber][1];
int blue = AVAILABLE_COLORS[randomNumber][2];
Serial.println("final colors");
Serial.println(red);
Serial.println(green);
Serial.println(blue);
Serial.println("-----------");
while (i < 255) {
if (irrecv.decode(&results)) {
if (existsInArray(results.value, AVAILABLE_CODES, 32) && results.value != INTENSITY_UP_CODE && results.value != INTENSITY_DN_CODE && results.value != FADE_CODE) {
Serial.println("return break");
return; break;
interpretRemoteCode(results.value);
} else if (results.value == INTENSITY_UP_CODE) {
raiseIntensity();
} else if (results.value == INTENSITY_DN_CODE) {
lowerIntensity();
}
irrecv.resume();
}
unsigned long currentMillis = millis();
int mappedSpeed = map(speedValue, 10, 1, 10, 100);
if (currentMillis - previousMillis >= mappedSpeed) {
previousMillis = currentMillis;
int mappedRedIntensity = map(i, 0, 255, 0, red);
int mappedRed = red != 0 ? map(red, 0, red, 0, mappedRedIntensity) : 0;
int mappedGreenIntensity = map(i, 0, 255, 0, green);
int mappedGreen = green != 0 ? map(green, 0, green, 0, mappedGreenIntensity) : 0;
int mappedBlueIntensity = map(i, 0, 255, 0, blue);
int mappedBlue = blue != 0 ? map(blue, 0, blue, 0, mappedBlueIntensity) : 0;
analogWrite(redPin, mappedRed);
analogWrite(greenPin, mappedGreen);
analogWrite(bluePin, mappedBlue);
i++;
}
}
while (i > 0) {
if (irrecv.decode(&results)) {
if (existsInArray(results.value, AVAILABLE_CODES, 32) && results.value != INTENSITY_UP_CODE && results.value != INTENSITY_DN_CODE && results.value != FADE_CODE) {
Serial.println("return break");
return; break;
interpretRemoteCode(results.value);
} else if (results.value == INTENSITY_UP_CODE) {
raiseIntensity();
} else if (results.value == INTENSITY_DN_CODE) {
lowerIntensity();
}
irrecv.resume();
}
unsigned long currentMillis = millis();
int mappedSpeed = map(speedValue, 10, 1, 10, 100);
if (currentMillis - previousMillis >= mappedSpeed) {
previousMillis = currentMillis;
int mappedRedIntensity = map(i, 0, 255, 0, red);
int mappedRed = red != 0 ? map(red, 0, red, 0, mappedRedIntensity) : 0;
int mappedGreenIntensity = map(i, 0, 255, 0, green);
int mappedGreen = green != 0 ? map(green, 0, green, 0, mappedGreenIntensity) : 0;
int mappedBlueIntensity = map(i, 0, 255, 0, blue);
int mappedBlue = blue != 0 ? map(blue, 0, blue, 0, mappedBlueIntensity) : 0;
analogWrite(redPin, mappedRed);
analogWrite(greenPin, mappedGreen);
analogWrite(bluePin, mappedBlue);
i--;
if (i == 0) {
fade();
}
}
}
}
int redVal = 0;
int grnVal = 0;
int bluVal = 0;
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 256 / step; // divide by 1020
}
return step;
}
int calculateVal(int step, int val, int i) {
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}
void crossFade() {
int randomNumber = random(16);
customLoop = true;
unsigned long previousMillis = 0;
// Convert to 0-255
int R = AVAILABLE_COLORS[randomNumber][0];
int G = AVAILABLE_COLORS[randomNumber][1];
int B = AVAILABLE_COLORS[randomNumber][2];
int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);
int i = 0;
while (i <= 256) {
if (irrecv.decode(&results)) {
if (existsInArray(results.value, AVAILABLE_CODES, 32) && results.value != INTENSITY_UP_CODE && results.value != INTENSITY_DN_CODE && results.value != SMOOTH_CODE) {
Serial.println("return break");
return; break;
interpretRemoteCode(results.value);
} else if (results.value == INTENSITY_UP_CODE) {
raiseIntensity();
} else if (results.value == INTENSITY_DN_CODE) {
lowerIntensity();
}
irrecv.resume();
}
unsigned long currentMillis = millis();
int mappedSpeed = map(speedValue, 10, 1, 50, 150);
if (currentMillis - previousMillis >= mappedSpeed) {
previousMillis = currentMillis;
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, grnVal);
analogWrite(bluePin, bluVal);
i++;
if (i == 257) {
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
crossFade();
}
}
}
}