fabianoriccardi / dimmable-light

Arduino library to manage dimmers compatible with AVR, ESP8266, ESP32, SAMD, and RP2040 platforms.
GNU Lesser General Public License v2.1
97 stars 29 forks source link

lamp wrongly dark under very specific conditions, Uno 8 lamps krida #57

Open distrakt opened 9 months ago

distrakt commented 9 months ago

Describe the bug

The sketch shows a bug I have seen in dimmable_lights.h

What I see is this:

If half the lamps are set to value X, and half are set to X+1, and digitalRead() is in the sketch (even uncalled!) then the ones set to X are displayed dark.

In this sketch I set them alternating ABABABAB, but I've seen same behavior for AAAABBBB; likely any order. If they're not half-and-half, the one with fewer works ok. (or maybe one with greater, my observations got confused)

Conditions:

Using 8 lamps on a Krida controller, on Arduino Uno and ONLY if a digitalRead() function call is present, even if the digitalRead() hasn't been called.

(Perhaps digitalRead() has some setup code or interrupt contention... ?)

Hardware and software used

Using board 'uno' from platform in folder: /Users/poly/Library/Arduino15/packages/arduino/hardware/avr/1.8.6 Using core 'arduino' from platform in folder: /Users/poly/Library/Arduino15/packages/arduino/hardware/avr/1.8.6


I'm pasting the sketch in here, apologies if that isn't the convention, this is my first github bug report! :)

/** david.van.brink@gmail.com 2024-02-23

This sketch shows a bug I have seen in dimmable_lights.h

What I see is this:

If half the lamps are set to value X, and half are set to X+1, then the ones set to X are displayed dark.

In this sketch I set them alternating ABABABAB, but I've seen same behavior for AAAABBBB; likely any order. If they're not half-and-half, the one with fewer works ok. (or maybe one with greater, my observations got confused)

Conditions:

Using 8 lamps on a Krida controller, on Arduino Uno and ONLY if a digitalRead() function call is present, even if the digitalRead() hasn't been called.

*/

include // library "Dimmable Light" 1.6

define ALL_EIGHT_LAMPS 1 // bug happens if using 8 lamps. fewer works ok.

define READPIN 12

define INCLUDE_A_DIGITAL_READ 1

// the 8 pins used by the Krida 8-channel dimmer DimmableLight dl1(11); DimmableLight dl2(10); DimmableLight dl3(9); DimmableLight dl4(8);

if ALL_EIGHT_LAMPS

DimmableLight dl5(7); DimmableLight dl6(6); DimmableLight dl7(5); DimmableLight dl8(4);

endif

void setup() { DimmableLight::setSyncPin(3); DimmableLight::begin(); pinMode(READPIN, INPUT_PULLUP);

Serial.begin(115200); delay(200); Serial.print("\n\n"); Serial.print("Dimmable Light bug with Krida 8 channel controller\n\n");

if INCLUDE_A_DIGITAL_READ

Serial.println("digitalRead() is linked in, we see bug of half lamps off in some cases");

else

Serial.println("no digitalRead(), all lamps appear on as desired & expected");

endif

}

/// set lamps alternating brightnesses a and b void setEm(uint8_t a, uint8_t b, uint16_t wait) { Serial.print("lamps "); Serial.print(a, HEX); Serial.print(", "); Serial.print(b, HEX); Serial.println("");

dl1.setBrightness(a); dl2.setBrightness(b); dl3.setBrightness(a); dl4.setBrightness(b);

if ALL_EIGHT_LAMPS

dl5.setBrightness(a); dl6.setBrightness(b); dl7.setBrightness(a); dl8.setBrightness(b);

endif

delay(wait); }

static int loops = 0; void loop() { loops++;

if INCLUDE_A_DIGITAL_READ

// this wont actually do a read for a long, long time // but just linking in the function seems // to cause the four-lights-dark bug. if(loops == 6000) { Serial.println("------> digital read happening! <------"); digitalRead(READPIN); }

endif

setEm(0x81, 0x80, 2000); // different by 1 step -- one lamp off setEm(0x80, 0x81, 2000); setEm(0x82, 0x80, 2000); // different by 2 steps, works ok

Serial.print(loops); Serial.println(" completed. let's go again."); }

distrakt commented 9 months ago

Completely acceptable workaround is to use FastGPIO library instead of the default arduino gpio functions.

But even still, I found some instability (slight occasional flicker) when lamps were near brightness to each other, especially trying to sweep them all together to a new brightness. A further workaround that helped is setBrightness(br & 0xfe), so lamps are not as close to each other in the interrupt timing.

This is on UNO; maybe not issue on faster cpu.