wilmouths / RGBLed

An Arduino library to control RGB led
GNU General Public License v3.0
42 stars 20 forks source link

COMMON_ANODE dont work correctly #5

Closed knuella closed 3 years ago

knuella commented 3 years ago

Hi,

on Arduino Nano 33 BLE sense I can not set the onboard RGB-LED with COMMON_ANODE. With some change in the color function it works for me:

void RGBLed::color(int red, int green, int blue) {
    if (_common == COMMON_ANODE) {
        analogWrite(_red, 255 - red);
        analogWrite(_green, 255 - green);
        analogWrite(_blue, 255 - blue);
    } else {
        analogWrite(_red, red);
        analogWrite(_green, green);
        analogWrite(_blue, blue);
    }
}

I also rewrite your fade function, it seems as it works without the >= / <= stuff:

void RGBLed::fade(int red, int green, int blue, int steps, int duration, int value) {
        float brightness = float(value) / 255.f;
        color(red * brightness, green * brightness, blue * brightness);
        delay((unsigned long) (duration / steps));
}
wilmouths commented 3 years ago

I just updated the color function so that it works with COMMON_ANODE, and I also modified the fade function as you indicated, but I kept the principle of checking the values, if it's less than 0 I put 0, if it's greater than 255 I put 255, which will avoid burning the LEDs.