// Forum: https://forum.arduino.cc/t/can-you-use-the-strip-fill-command-to-fill-from-the-opposite-direction/1189165/
// This Wokwi project: https://wokwi.com/projects/381398312933908481
#include <Adafruit_NeoPixel.h>
#define LED_PIN 2
#define NUM_PIXELS 108
Adafruit_NeoPixel strip(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
uint32_t r = strip.Color(255, 0, 0);
uint32_t g = strip.Color(0, 255, 0);
uint32_t b = strip.Color(0, 0, 255);
uint32_t rgb = strip.Color(255, 255, 255);
int PWMvalue[6]; // default all 0
// For the "more fun" section
int column = 1;
int direction = +1;
void setup()
{
Serial.begin(115200);
strip.begin();
strip.clear();
// identify the first pixel of each strip
Serial.println("Identifying the first leds of the ledstrips");
for(int i=0; i<6; i++)
{
strip.setPixelColor(i*18,r);
strip.show();
delay(350);
}
delay(700);
strip.clear();
// fill 108 pixels with fill.
unsigned long t1, t2, elapsed;
t1 = micros();
strip.fill(b, 0, 108); // color, offset, number of leds
t2 = micros();
elapsed = t2 - t1;
Serial.print("strip.fill (all 108 leds) took ");
Serial.print(elapsed);
Serial.println(" µs");
// fill 108 pixels strip.setPixelColor.
t1 = micros();
for(int i=0; i<108; i++)
{
strip.setPixelColor(i, b); // index, color
}
t2 = micros();
elapsed = t2 - t1;
Serial.print("strip.setPixelColor (all 108 leds) took ");
Serial.print(elapsed);
Serial.println(" µs");
// Measure how long the clear takes
t1 = micros();
strip.clear();
t2 = micros();
elapsed = t2 - t1;
Serial.print("strip.clear took ");
Serial.print(elapsed);
Serial.println(" µs");
// Measure how much sending the pulse train takes
t1 = micros();
strip.show();
t2 = micros();
elapsed = t2 - t1;
Serial.print("Updating the ledstrip (all 108 leds) took ");
Serial.print(elapsed);
Serial.println(" µs");
strip.clear();
}
void loop()
{
// The strip.fill() goes nuts if the number of leds is 0
// Keep the amount from 1 up to 18 (inclusive).
for(int i=0; i<6; i++)
{
PWMvalue[i] = random(1,19); // 1...18
}
strip.clear();
strip.fill(r, 0, PWMvalue[0]);
strip.fill(g, 18, PWMvalue[1]);
strip.fill(b, 36, PWMvalue[2]);
strip.fill(r, 54, PWMvalue[3]);
strip.fill(g, 72, PWMvalue[4]);
strip.fill(b, 90, PWMvalue[5]);
// for fun, fill the remaning leds with a different color
strip.fill(strip.Color(120,120,0), 90 + PWMvalue[5], 18 - PWMvalue[5]);
// "more fun"
// horizontal moving of five white leds
for(int i=7; i<=11; i++)
{
strip.setPixelColor(column*18+i, strip.Color(255,255,255));
}
column += direction;
if( column == 0 || column == 5)
direction = -direction;
strip.show();
delay(200);
}