wilmouths / RGBLed

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

Support for multiple LED's #15

Open JeanSolagnier opened 4 months ago

JeanSolagnier commented 4 months ago

Hi there. I am trying to control 3x CA RGB leds but it appears to be impossible.

#define R1_PIN  12
#define G1_PIN  14
#define B1_PIN  27
#define R2_PIN  26
#define G2_PIN  25
#define B2_PIN  33
#define R3_PIN  32
#define G3_PIN  13
#define B3_PIN  19

// Create instances of the RGBled class for each LED
RGBLed led1(R1_PIN, G1_PIN, B1_PIN, RGBLed::COMMON_ANODE);
RGBLed led2(R2_PIN, G2_PIN, B2_PIN, RGBLed::COMMON_ANODE);
RGBLed led3(R3_PIN, G3_PIN, B3_PIN, RGBLed::COMMON_ANODE);

led1.setColor(255, 0, 0);
led2.setColor(0, 255, 0);
led3.setColor(0, 0, 255);

Setting led3 to blue sets all 3 LED's to blue.

I am using an esp32.

Can this library only handle one led?

kind regards, Jean

JeanSolagnier commented 4 months ago

I narrowed the problem down to the removed functions "ledcSetup" and "ledcAttachPin". they are now merged together into "ledcAttach". https://docs.espressif.com/projects/arduino-esp32/en/latest/migration_guides/2.x_to_3.0.html#id2

one way to fix this is to manually pass in channel numbers into the function like this:

RGBLed led1(0, 1, 2, R1_PIN, G1_PIN, B1_PIN, RGBLed::COMMON_ANODE);
RGBLed led2(3, 4, 5, R2_PIN, G2_PIN, B2_PIN, RGBLed::COMMON_ANODE);
RGBLed led3(6, 7, 8, R3_PIN, G3_PIN, B3_PIN, RGBLed::COMMON_ANODE);
RGBLed::RGBLed(int ch1, int ch2, int ch3, int red, int green, int blue, bool common) : _ch1(ch1), _ch2(ch2), _ch3(ch3), _red(red), _green(green), _blue(blue), _common(common), _brightness(100)
{
#if defined(ESP32)
    ledcSetup(_ch1, 5000, 8);
    ledcSetup(_ch2, 5000, 8);
    ledcSetup(_ch3, 5000, 8);

    ledcAttachPin(_red, _ch1);
    ledcAttachPin(_green, _ch2);
    ledcAttachPin(_blue, _ch3);

#else
    pinMode(_red, OUTPUT);
    pinMode(_green, OUTPUT);
    pinMode(_blue, OUTPUT);
#endif
}

The proper way would be to use the new esp32 IDF function, but I get compile errors. saying that 'ledcAttach' was not declared in this scope.