I've moved the analogWrite
part of the code inside the switch case
BRIGHTNESS_UP
& BRIGHTNESS_DOWN
and it's working. I also removed the crossFade(c);
line.
Here's the working code:
#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;
int c[3];
//RGB Pins Array
int CH[3] = {11, 12, 13};
int val[3] = {0, 0, 0}; // led brightness 0-255
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 = 100; // 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
#define MAX 255
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
//initiate rgb pins output
for (int i=0; i<3; i++)
{
pinMode(CH[i], 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 UP
modus = 0;
brightness += 5;
if (brightness > 255) brightness = 255;
c[0] = prevR; c[1] = prevG; c[2] = prevB;
analogWrite(ledr, redVal * brightness / 255); // Write current values to LED pins
analogWrite(ledg, grnVal * brightness / 255);
analogWrite(ledb, bluVal * brightness / 255);
break;
case BRIGHTNESS_DOWN: //DIMMING DOWN
modus = 0;
brightness -= 5;
if (brightness < 0) brightness = 0;
c[0] = prevR; c[1] = prevG; c[2] = prevB;
analogWrite(ledr, redVal * brightness / 255); // Write current values to LED pins
analogWrite(ledg, grnVal * brightness / 255);
analogWrite(ledb, bluVal * brightness / 255);
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();
}
if(modus==2){ // Querry pb Modus:1
}
Serial.println(prevR);
Serial.println(prevG);
Serial.println(prevB);
// Serial.println(results.value, HEX);
// Serial.println(DEC);
// Serial.println(DEC);
// Serial.println(DEC);
// Serial.print("channel 1,2,3 values:"); // sends brightness values to the serial monitor
// for(int i=0; i<3; i++){ // every time the remote is pressed
// Serial.print(CH[i]);
// Serial.print(" ");
// }
}
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
}
thanks geometrikal for your support! You believe it's possible to manipulate the dimming of the fade effect too? It's using the RGBMood class to create a HSB color mix.