0

I wish to blink all the strip. In the code below it just blink the first LED, how can I fix it?

Code:

#include <Adafruit_NeoPixel.h>


#define PIN 6
#define NUM_LEDS 24


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


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

void loop() {
  strip.setPixelColor(0, 255, 255, 255);
  strip.show();
  delay(1000);
  strip.setPixelColor(0, 0, 0, 0);
  strip.show();
  delay(1000);
}
2

The first argument of setPixelColor is is the number of the led. In your case 0, so the first led.

Instead use a for loop to set all the leds from 0 until (excluding) 24 (which is stored in NUM_LEDS).

void loop()
{
  for (int led = 0; led < NUM_LEDS ; led++)
  {
    strip.setPixelColor(led, 255, 255, 255);
  }
  strip.show();
  delay(1000);

  for (int led = 0; led < NUM_LEDS ; led++)
  {
    strip.setPixelColor(led, 255, 255, 255);
  }
  strip.show();
  delay(1000);

To make the code better (shorter, but more important more maintainable, and reducing code duplication), create a separate function:

void loop() 
{
  setLeds(255);
  setLeds(0);
}

void setLeds(int intensity)
{
  for (int led = 0; led < NUM_LEDS ; led++)
  {
    strip.setPixelColor(led, intensity, intensity, intensity);
  }

  strip.show();
  delay(1000);
}
  • Thanks. In the longer script I presume it should be strip.setPixelColor(led, 0, 0, 0); – dkin Sep 20 at 10:28
  • Yes, because the argument intensity decides when values 0 or 255 are used. – Michel Keijzers Sep 20 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.