Page 1 of 1
Forum

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 ...
 
Share:
Notifications
Clear all

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

2 Posts
2 Users
0 Likes
1,921 Views
(@Anonymous)
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);

  delay(500);

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

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

  delay(500);

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

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

    digitalWrite(relay, LOW); // turn off relay
    delay(100);
  }
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2663
 

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);
    pixels.show();
}

 


   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: