gradually filling a led strip like tetris ?

hello i made this working with 1 led only. how should i modify the code in order to have more than 1 led moving? lets say 4 for example. same sequence, but 4 leds each time instead of 1.

check attached link for video example.

arduino gradual fill test video

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUM_PIXELS 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.setBrightness(30); 
  strip.begin();
  strip.show(); 
}

void loop() {
  int i, j;

 for (uint16_t i = 0; i < strip.numPixels() ; i++) {
   int r = (255); 
   int g = (50);
   int b = (0);

    for (uint16_t j = 0; j < strip.numPixels() - i; j++) {
      strip.setPixelColor(j - 1, strip.Color(0, 0, 0)); 
      strip.setPixelColor(j, strip.Color(r, g , b)); 
      strip.show();
      delay(5);
    }

  }
}
strip.setPixelColor(j, strip.Color(r, g , b));

If you could only make “j” bigger in that line.. even better if you incremented it bigger.

i tried strip.setPixelColor(j++, strip.Color(r, g , b));

and other stuff but the result is not the one needed.

mitsakos:
i tried strip.setPixelColor(j++, strip.Color(r, g , b));

and other stuff but the result is not the one needed.

You need to call that function n times to make n pixels light up.

   int r = (255);
   int g = (50);
   int b = (0);

(The) (parentheses) (add) (no) (value).

    for (uint16_t j = 0; j < strip.numPixels() - i; j++) {
      strip.setPixelColor(j - 1, strip.Color(0, 0, 0));
      strip.setPixelColor(j, strip.Color(r, g , b));
      strip.show();
      delay(5);
    }

Here, you are turning off one pixel before turning on another pixel. To have more than one pixel on, clearly the - 1 needs to be - 2 or - 3 or - 4… You need to make sure that j - n is never less than 0, though.

PaulS:
You need to call that function n times to make n pixels light up.

   int r = (255);

int g = (50);
  int b = (0);



(The) (parentheses) (add) (no) (value).



for (uint16_t j = 0; j < strip.numPixels() - i; j++) {
      strip.setPixelColor(j - 1, strip.Color(0, 0, 0));
      strip.setPixelColor(j, strip.Color(r, g , b));
      strip.show();
      delay(5);
    }



Here, you are turning off one pixel before turning on another pixel. To have more than one pixel on, clearly the - 1 needs to be - 2 or - 3 or - 4... You need to make sure that j - n is never less than 0, though.

this lights up only the first 4 leds in the sequence, the rest still come 1 by 1. i tried the j -4

this lights up only the first 4 leds in the sequence, the rest still come 1 by 1. i tried the j -4

Show the code. How are you making sure that j - 4 is a valid value to use in the call to setPixelColor()?

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUM_PIXELS 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 300;

void setup() {
  strip.setBrightness(30); 
  strip.begin();
  strip.show(); 
  
}


void loop() {
  int i, j;
  delay(delayval);
  

 for (uint16_t i = 0; i < strip.numPixels() ; i++) { // iterate over every LED of the strip
   int r = (255); 
   int g = (50);
   int b = (0);
   

    for (uint16_t j = 0; j < strip.numPixels() - i; j++) {
      
      strip.setPixelColor(j - 4, strip.Color(0, 0, 0)); 
      strip.setPixelColor(j , strip.Color(r, g , b));
      strip.show();
      delay(5);
     
      
    }

  }
}

i am not sure about your latest response, i am amateur at these stuff.

    for (uint16_t j = 0; ....

strip.setPixelColor(j - 4

So when j starts at zero, what pixel does j-4 refer to?

Don't attempt to set non-existent pixels. This can crash your Arduino because you are writing to memory locations used for other purposes. There is no automatic protection against this.

MorganS:
So when j starts at zero, what pixel does j-4 refer to?

Don't attempt to set non-existent pixels. This can crash your Arduino because you are writing to memory locations used for other purposes. There is no automatic protection against this.

i am not sure i understand your question aswell. any guidance/help would be awesome. when i set the

strip.setPixelColor(j - 1/2/3/4 the 1st row of leds that light up correspond to that value. if i use j - 3, 3 led fill the row and then 1 by 1

j -4 example

    for (uint16_t j = 0; j < strip.numPixels() - i; j++) {
     
      strip.setPixelColor(j - 4, strip.Color(0, 0, 0));
      strip.setPixelColor(j , strip.Color(r, g , b));

should be

    for (uint16_t j = 0; j < strip.numPixels() - i; j++)
    {
      if(j >= 4)
         strip.setPixelColor(j - 4, strip.Color(0, 0, 0));
      strip.setPixelColor(j , strip.Color(r, g , b));

This will set the 0 pixel on, then set the 1 pixel on, then set the 2 pixel on, then set the 3 pixel on, then set the 4 pixel on AND the 0 pixel off, then set the 5 pixel on AND the 1 pixel off…

MorganS:
Don't attempt to set non-existent pixels. This can crash your Arduino because you are writing to memory locations used for other purposes. There is no automatic protection against this.

There is !! he is using adafruit_neopixel !!

To let the thing rotate at the end you can make use of the % modulo

    for (uint16_t j = 0; j < strip.numPixels() - i; j++)
    {
      strip.setPixelColor(j , strip.Color(0, 0, 0));
      strip.setPixelColor((j + 4) % strip.numPixels() , strip.Color(r, g , b));
     }