Strange behavior without Serial.print()

Hi all,

I have a strange behavior with my code. I use FastLed to blink a ledstrip.

I have a function in a Ledstrip.cpp file :

void LedStrip::setPixel(uint32_t num, int color)
{
	
Serial.println(num);
	leds[num].b=(color & 0xFF);  // Take just the lowest 8 bits.
	leds[num].g=((color >> 8)& 0xFF);  // Shift the integer right 8 bits.
	leds[num].r=((color >> 16)& 0xFF);  // Shift the integer right 8 bits.
}

In the main ino file, I do in the setup :

uint32_t configuredStripLength=144;
...
...
        for(uint32_t i=0;i<configuredStripLength;i++){
          ledstrip.setPixel(i, 255, 0, 0);
        }

It works and the leds are red from 0 to 144
If I remove Serial.println(num);, the strip is not illuminated or illuminated but not at 0 maybe 120

I need your help :slight_smile:

kakou:

void LedStrip::setPixel(uint32_t num, int color)
          ledstrip.setPixel(i, 255, 0, 0);

Those functions are not a match, and it is far better to just post all your code instead of trying to guess in ignorance where the problem might be.

Sorry, I posted the code on pastbin (it exceeds 9000 char)
The full code (Used on wemos D1)
ArduinoLedStrip

kakou:

void LedStrip::setPixel(uint32_t num, int color)

{

Serial.println(num);
leds[num].b=(color & 0xFF);  // Take just the lowest 8 bits.
leds[num].g=((color >> 8)& 0xFF);  // Shift the integer right 8 bits.
leds[num].r=((color >> 16)& 0xFF);  // Shift the integer right 8 bits.
}

Despite what the comment says, you are shifting a 16-bit int right by 16 bits. All of the data will shift off the end, leaving nothing for Red. If the high bit of Green is a 1 you might end up with 255 (sign extension), otherwise 0.

Yes the comment are bad :), but the code works for the colors (I can use red, green and blue without problems). The only problem is the value of num, if I print it before with Serial.println and I call ...setPixel(0,...) The first led illuminates, If I remove the println, The led ~120 illuminates. It's like a problem of cast without the Serial.println

kakou:
Yes the comment are bad :), but the code works for the colors (I can use red, green and blue without problems).

In that case you have a problem not only with this part of the program, but also the part that uses leds[] to actually output to the LEDs. Because, reply #4 is correct, that code can not possibly work.

In fact, on a wemos (ESP8266) an int is 32 bit long, so the comment are good :slight_smile:

Output on ESP8266 (32bit)

sizeof(char)=1
sizeof(int)=4
sizeof(short)=2
sizeof(long)=4
sizeof(float)=4
sizeof(double)=8
sizeof(uint8_t)=1
sizeof(uint16_t)=2
sizeof(uint32_t)=4

Any idea ? why with printing the num is the setpixel() function this code work but without, the code do anything ...

Try adding a FastLED.setDither( 0 ); to your void LedStrip::begin(void) and see if there is any behaviour change.

Otherwise, can you slim your ArduinoStripController.ino down to the minimum required to reproduce the problem and post it. Please also accurately describe what happens in the slimmed down version.

Thanks, I will try