Skip to main content
added 317 characters in body
Source Link
geometrikal
  • 2.9k
  • 15
  • 21

Edit 2:

I think that what might be happening is that the color is reduced by brightness, then that reduced value is saved in prevR/G/B when actually it should just be the full brightness value. Try changing last bit of crossFade to :

prevR = color[0]; 
prevG = color[1]; 
prevB = color[2];

Edit 2:

I think that what might be happening is that the color is reduced by brightness, then that reduced value is saved in prevR/G/B when actually it should just be the full brightness value. Try changing last bit of crossFade to :

prevR = color[0]; 
prevG = color[1]; 
prevB = color[2];
added 821 characters in body
Source Link
geometrikal
  • 2.9k
  • 15
  • 21

Edit

Different approach:

Global variable for brightness:

int brightness = 100;
int c[3];

Change this value each time:

case BRIGHTNESS_UP:
    modus = 0;
    brightness += 5;
    if (brightness > 255) brightness = 255;
    c[0] = prevR; c[1] = prevG; c[2] = prevB;
    crossFade(c);
    break;

case BRIGHTNESS_DOWN:
    modus = 0;
    brightness -= 5;
    if (brightness < 0) brightness = 0;
    c[0] = prevR; c[1] = prevG; c[2] = prevB;
    crossFade(c);
    break;

Then in crossFade we change the last few lines:

analogWrite(ledr, redVal * brightness / 255);   // Write current values to LED pins
analogWrite(ledg, grnVal * brightness / 255);      
analogWrite(ledb, bluVal * brightness / 255); 

Edit

Different approach:

Global variable for brightness:

int brightness = 100;
int c[3];

Change this value each time:

case BRIGHTNESS_UP:
    modus = 0;
    brightness += 5;
    if (brightness > 255) brightness = 255;
    c[0] = prevR; c[1] = prevG; c[2] = prevB;
    crossFade(c);
    break;

case BRIGHTNESS_DOWN:
    modus = 0;
    brightness -= 5;
    if (brightness < 0) brightness = 0;
    c[0] = prevR; c[1] = prevG; c[2] = prevB;
    crossFade(c);
    break;

Then in crossFade we change the last few lines:

analogWrite(ledr, redVal * brightness / 255);   // Write current values to LED pins
analogWrite(ledg, grnVal * brightness / 255);      
analogWrite(ledb, bluVal * brightness / 255); 
Source Link
geometrikal
  • 2.9k
  • 15
  • 21

You don't have a delay in the fade loop, this goes from 0 to 255 almost instantly:

for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledg, fadeValue);    
}

But I'm not sure you want to fade from off to on in one go. If you want to just increment and decrement the brightness:

First notice that prevR, prevG, prevB hold the current led values. So we shall use them.

Change the switch case to:

case BRIGHTNESS_UP:
    modus = 0;
    int c[3];
    // decrease by 0.9*value but also take off 2 so we get to zero
    c[0] = (prevR-2) * 90 / 100;
    c[1] = (prevG-2) * 90 / 100;
    c[2] = (prevB-2) * 90 / 100;
    for( int j = 0; j < 3; j++ ) {
        if (c[j] < 0) c[j] = 0;
    }
    crossFade(c);
    break;

case BRIGHTNESS_DOWN:
    modus = 0;
    int c[3];
    // increase value by 1/0.9 but also add 2 so we get off zero
    c[0] = (prevR+2) * 100 / 90 + 2;
    c[1] = (prevG+2) * 100 / 90 + 2;
    c[2] = (prevB+2) * 100 / 90 + 2;
    for( int j = 0; j < 3; j++ ) {
        if (c[j] > 100) c[j] = 100;
    }
    crossFade(c);
    break;

I can't check this to see if it works see please test and see.