I have this sketch:
#include <TimerOne.h>
#include <IRremote.h>
#include <RGBMood.h>
int RECV_PIN = 2; // IR-Receiver PIN
int led = 13; // Satus-LED PIN
int modus; // Modus for Interrupt-Querry
int ledr = 11; // RGB LED red PIN
int ledg = 12; // RGB LED green PIN
int ledb = 13; // RGB LED blue PIN
int SerialBuffer = 0;
RGBMood m(ledr, ledg, ledb);
int timerwert = 20; // Timer time for Interrupt in ms
String readString;
// Color arrays
int black[3] = { 0, 0, 0 };
int white[3] = { 100, 100, 100 };
int red[3] = { 100, 0, 0 };
int green[3] = { 0, 100, 0 };
int blue[3] = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// etc.
// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];
int wait = 10; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 3; // How many times should we loop before stopping? (0 for no stop)
int j = 0; // Loop counter for repeat
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
#define ON 0xF4F37A66
#define OFF 0x1363ADB4
#define BRIGHTNESS_UP 0xE6721691
#define BRIGHTNESS_DOWN 0xE9721B48
#define FLASH 0xFFF00F
#define STROBE 0xFFE817
#define FADE 0x39ED1255
#define SMOOTH 0xFFC837
#define RED 0x9D561314
#define GREEN 0XCB8E93A5
#define BLUE 0xC88E8EEC
#define WHITE 0x16DBBEE3
#define ORANGE 0xFFB04F
#define YELLOW_DARK 0xFFA857
#define YELLOW_MEDIUM 0xFF9867
#define YELLOW_LIGHT 0xFF8877
#define GREEN_LIGHT 0XFF30CF
#define GREEN_BLUE1 0XFF28D7
#define GREEN_BLUE2 0XFF18E7
#define GREEN_BLUE3 0XFF08F7
#define BLUE_RED 0XFF708F
#define PURPLE_DARK 0XFF6897
#define PURPLE_LIGHT 0XFF58A7
#define PINK 0XFF48B7
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
pinMode(ledr, OUTPUT); // Set RGB LED Pins as Output
pinMode(ledg, OUTPUT); // Set RGB LED Pins as Output
pinMode(ledb, OUTPUT); // Set RGB LED Pins as Output
pinMode(led, OUTPUT); // set Status-LED as Output
m.setMode(RGBMood::RANDOM_HUE_MODE); // Automatic random fade.
m.setHoldingTime(4000); // Keep the same color for 4 seconds before fading again.
m.setFadingSteps(150); // Fade with 150 steps.
m.setFadingSpeed(50); // Each step last 50ms. A complete fade takes 50*150 = 7.5 seconds
m.setHSB(random(359), 255, 255);
Serial.begin(9600);
irrecv.enableIRIn(); // Start of IR-Recive
Timer1.initialize(timerwert); // Initialisation of Timer-Interrupts
Timer1.attachInterrupt(leseIR); // IR-Read from Interrupt
}
void leseIR(){
if (irrecv.decode(&results)){
irrecv.resume(); // Receive the next value
switch (results.value) {
case FADE: // Modus Fade (DIY 4)
modus = 1;
break;
case 0xFF906F: // Modus pcambi (DIY 5)
modus = 2;
break;
case ON: //Power
modus = 0;
crossFade(white); // RGB LEDs Off
break;
case OFF: //Power
modus = 0;
crossFade(black); // RGB LEDs Off
break;
case BLUE: //Blau 0,0,255
modus = 0;
crossFade(blue);
break;
case RED: //Rot
modus = 0;
crossFade(red);
break;
case GREEN://Grün
modus = 0;
crossFade(green);
break;
case WHITE: //Weiss
modus = 0;
crossFade(white);
break;
case BRIGHTNESS_UP: //DIMMING
modus = 0;
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledg, fadeValue);
}
break;
case BRIGHTNESS_DOWN: //orange
modus = 0;
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledg, fadeValue);
}
break;
case 0xFFAA55://Grün mitrtel
modus = 0;
break;
case 0xFF926D: //blau mittel
modus = 0;
break;
case 0xFF12ED: //rosa
modus = 0;
break;
} // Switch END
}
}
void loop() {
if(modus==1){ // Querry pb Modus:1
m.tick();
}
Serial.println(results.value, HEX);
Serial.println(DEC);
Serial.println(DEC);
Serial.println(DEC);
}
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}
/* The next function is calculateVal. When the loop value, i,
* reaches the step size appropriate for one of the
* colors, it increases or decreases the value of that color by 1.
* (R, G, and B are each calculated separately.)
*/
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;
}
/* crossFade() converts the percentage colors to a
* 0-255 range, then loops 1020 times, checking to see if
* the value needs to be updated each time, then writing
* the color values to the correct pins.
*/
void crossFade(int color[3]) {
// Convert to 0-255
int R = (color[0] * 255) / 100;
int G = (color[1] * 255) / 100;
int B = (color[2] * 255) / 100;
int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);
for (int i = 0; i <= 1020; i++) {
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);
analogWrite(ledr, redVal); // Write current values to LED pins
analogWrite(ledg, grnVal);
analogWrite(ledb, bluVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
// Update current values for next loop
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}
It's working like a charm, the fade operates smoothly using timer1
, the colors appear correct on led, but I can't fix the brightness part. Can someone provide sample code to do this?
If anyone else wants to use the code, I'm using an Arduino Mega 2560, please use the same PINs as mine since timers works only on 11,12,13 on this Arduino. (link with pin info regarding several Arduino boards)
I'm using a small Xbox 360 IR control (no numbers keypad), some HEX codes are left intact from the original sketch found on internet.