Page 1 of 1

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!

fill() not working ...
Clear all

[Solved] fill() not working correctly on RGBw Neopixel strip

2 Posts
2 Users
Joined: 1 second ago
Posts: 0
Topic starter  

I am trying to light a string of RGBw LEDs red with no flashing, chasing, etc.. With the code I am using, the first LED lights up red followed by green then blue. What am I doing incorrectly?

#include <Adafruit_NeoPixel.h>

#define PIN 6 // input pin Neopixel is attached to

#define NUMPIXELS 4 // number of neopixels in strip

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

const int button = 9;
int buttonState = 0;

int relay = 12;

void setup() {
  pinMode(relay, OUTPUT);

  pinMode(button, INPUT);


  pixels.begin(); // Initialize the NeoPixel library.;

void loop() {
  buttonState = digitalRead(button);


  if (buttonState == HIGH) {
    digitalWrite(relay, HIGH); // turn on relay

    uint32_t red = pixels.Color(255, 0, 0, 0);
    pixels.fill(red, 0, 4);;

    digitalWrite(relay, LOW); // turn off relay

Famed Member Admin
Joined: 11 years ago
Posts: 2680

I'm not a big fan of the NeoPixel library (I prefer FastLED), but this fill function should do the trick indeed.

First thing I'd look at is the count you're using. 
When you have 4 LEDs, the count is 0, 1, 2, 3. So there is no "4". A common mistake (I make that mistake every now and then as well).
This is why one often does write something like this:

pixels.fill(red, 0, NUMPIXELS-1);

Advantage is then additionally that you only have to change the value for NUMLEDS and your entire sketch automatically adapts to that.

If that still doesn't fix it, then I'd check and see if the other items (button) interfere.

For example: Does this code work?

void loop() {
    uint32_t red = pixels.Color(255, 0, 0, 0);
    pixels.fill(red, 0, NUMPIXELS-1);;