Problem with first three LEDs on WS2812 strip

Good evening, I have a problem with some ws2812 LEDs, I'm trying to make a 7-segment number, but when I go to farm the numbers the first 3 LEDs always stay on even when they shouldn't, I tried to test only the first three LEDs even individually and they respond to commands, but when I send this sketch, any number I ask them to make the first three always on, could you help me understand what the problem could be? thank you very much below is the sketch I'm using, thanks again.`

#include <Adafruit_NeoPixel.h>

#define LED_PIN 6
#define NUM_LEDS 21  // 7 segmenti * 3 LED per segmento

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

// Mappatura dei segmenti per i numeri 0-9
const int segment[10][7] = {
    {0, 1, 2, 3, 4, 5},  // 0
    {1, 2 },              // 1
    {0, 1, 3, 4, 6},     // 2
    {0, 1, 2, 3, 6},     // 3
    {1, 2, 5, 6},        // 4
    {0, 2, 3, 5, 6},     // 5
    {0, 2, 3, 4, 5, 6},  // 6
    {0, 1, 2},           // 7
    {0, 1, 2, 3, 4, 5, 6}, // 8
    {0, 1, 2, 3, 5, 6}   // 9
};

// Imposta qui il numero iniziale
int currentNumber = 2; // Cambia questo valore per impostare il numero iniziale

void setup() {
    strip.begin();
    strip.show(); // Spegni tutti i LED
}

void loop() {
    updateDisplay(currentNumber);
    delay(1000); // Aspetta un secondo
}

void updateDisplay(int number) {
    // Spegni tutti i LED
    for (int i = 0; i < NUM_LEDS; i++) {
        strip.setPixelColor(i, strip.Color(0, 0, 0)); // Spegni il LED
    }

    // Accendi i segmenti per il numero corrente
    for (int i = 0; i < 7; i++) {
        int segmentIndex = segment[number][i];
        for (int j = 0; j < 3; j++) {
            int ledIndex = segmentIndex * 3 + j; // Ogni segmento ha 3 LED
            if (ledIndex < NUM_LEDS) {
                strip.setPixelColor(ledIndex, strip.Color(255, 0, 0)); // Rosso
            }
        }
    }
    strip.show();
}

This is still in the English section... so I'm not in trouble, right?

I think the first three WS2812s have died. You can replace them by cutting and soldering three more (Vcc to Vcc, GND to GND, DOUT to DIN). When you connect your data wire, be sure to have a 470 ohm resistor to limit current during "ON" phase and brightness changes, and also a 1000uF electrolytic capacitor across VCC and GND of the WS2812,

thanks for the reply, I have already changed the first three LEDs but nothing they always stay on, but as I said I tried to control only the first three by making them turn on and off and so they work, only when I send this sketch they always stay on, anyway now I will try to put the capacitor and the resistor as you advised me

Definitely hardware. Probably after the first three. Here is your code, working...

(edit... pardon the "not shaped as a 7seg"... I like to see it working before making it look good)

Would the first three LEDs be as if segmentIndex = 0 and j = 0,1,2?

If so, it might be from the zeros on the ragged side of this array:

Thanks so much for the reply, I'm not very expert yet, I'm trying to learn, could you tell me how I should change it? A thousand thanks.

I don't have your hardware, or an idea of your desired end result, but the array has space reserved for a fulll 10x7=70 integers, and all the cells that are not explicitly initialized are initialized to zero by default.

So maybe you should fill out the array to put a 99 or something in the empty places, and then if segmentIndex== 99, read that as 'off' and do not turn on a LED.

I have 21 LEDs in total, divided into pieces of three LEDs each, and I wanted these three LEDs to form a segment of the number, so how should I modify the sketch?

Add a serial.print to see what you have there.

from the serial print the first three LEDs (segment 0) appear to be always active

If I'm right about #11, there's probably other zeros printed out later than the first three iterations.

OK, decoding your code and text, the WS2812 led array scans across the 0th segment in each of three digits, and then the 1st digit of the three segments, and so on....

I think I'd mark the un-lit segments in your array with -1 as per #11, turn your loop inside out and make the outer loop digit rather than segment, and as you are cycling through the segments of that digit, choose not to light the unlit segments.

In order for your sketch to work as it is, the loop below can't go to i<7.

It has to go until i < the number of elements that segment[number] has. For example: the number 4 (segment[4]) has 4 elements. So, the loop should run until i<4.

I tried to use sizeof(), but it didn't work... So I would follow @DaveX suggestion in post #15

I liked your idea! Tried to put the strips in order, but the wires are a mess. Also had to change some of the OP's segments order.

Mega7sega

1 Like

thank you very much for your help, as I said I'm not an expert, could you please write me the part to modify and tell me where to replace it please, it would be of great help to me, thank you very much!

Could you please write me the part to be changed and tell me where to replace it? I would be infinitely grateful, I'm not very expert, thanks again.

Here's a version that cycles through the digits and turns off the first three LEDs for the number 4:

#include <Adafruit_NeoPixel.h>

#define LED_PIN 6
#define NUM_LEDS 21  // 7 segmenti * 3 LED per segmento

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

// Mappatura dei segmenti per i numeri 0-9
const int segment[10][7] = {
  {0, 1, 2, 3, 4, 5},  // 0
  {1, 2 },              // 1
  {0, 1, 3, 4, 6},     // 2
  {0, 1, 2, 3, 6},     // 3
  {1, 2, 5, 6, -1, -1, -1},        // 4
  {0, 2, 3, 5, 6},     // 5
  {0, 2, 3, 4, 5, 6},  // 6
  {0, 1, 2},           // 7
  {0, 1, 2, 3, 4, 5, 6}, // 8
  {0, 1, 2, 3, 5, 6}   // 9
};

// Imposta qui il numero iniziale
int currentNumber = 2; // Cambia questo valore per impostare il numero iniziale

void setup() {
  strip.begin();
  strip.show(); // Spegni tutti i LED
}

void loop() {
  updateDisplay(currentNumber++);
  delay(1000); // Aspetta un secondo
  if (currentNumber >9) currentNumber = 0; 
 }

void updateDisplay(int number) {
  // Spegni tutti i LED
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0)); // Spegni il LED
  }

  // Accendi i segmenti per il numero corrente
  for (int i = 0; i < 7; i++) {
    int segmentIndex = segment[number][i];
    for (int j = 0; j < 3; j++) {
      if(segmentIndex >= 0 ){
      int ledIndex = segmentIndex * 3 + j; // Ogni segmento ha 3 LED
      if (ledIndex < NUM_LEDS) {
        strip.setPixelColor(ledIndex, strip.Color(255, 0, 0)); // Rosso
      }
      }
    }
  }
  strip.show();
}

It seemed to work dropped into in @xfpd's simulation in #8.

You'd need to mark the unwanted other default "0"s in your array as negative numbers.

It works perfectly, thanks so much for the help!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.