Pulsing/Fading LED Strip

Hello All!

Pretty new to Arduino coding and looking for some assistance on smoothly pulsing/fading an led strip.

I am using the Adafruit Neopixel Library and I am trying to achieve the following:

  • A blue fading led strip

I found the following code, however when I make the adjustments the blue keeps pulsing/fading to white instead of staying blue and slowly pulsing/fading them to off and back on.

#include <Adafruit_NeoPixel.h>

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit... if you must, connect GND first.

void setup() {
  strip.begin();
  strip.show(); // initialize all pixels to "off"
}

void loop() {
  brighten();
  darken();
}

// 0 to 255
void brighten() {
  uint16_t i, j;

  for (j = 0; j < 255; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j, 255);
    }
    strip.show();
    delay(15);
  }
  //delay(100);
}

// 255 to 0
void darken() {
  Serial.begin(9600);
  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j, j);
    }
    strip.show();
    delay(15);
    Serial.println(j);
  }
  delay(100);
}

I've tried playing around with the strip.setPixelColor(i, j, j, j) settings for both brighten and darken but cant seem to get it to stay one consistent color of blue.

Any help would be appreciated.

      strip.setPixelColor(i, j, j, 255);

This looks wrong. If the last 3 parameters are the R, G, B values for LED i and you only want to fade blue up or down in brightness then should you not keep R and G as zero and vary the value of the blue parameter ?

UKHeliBob:

      strip.setPixelColor(i, j, j, 255);

This looks wrong. If the last 3 parameters are the R, G, B values for LED i and you only want to fade blue up or down in brightness then should you not keep R and G as zero and vary the value of the blue parameter ?

Thank you that actually worked!

I am running into another issue now, while the leds slowly fade to off, when they come back on it comes back on to full brightness, how can I get this to slowly fade back on the same speed it fades to off?

Post your code as it is now

#include <Adafruit_NeoPixel.h>

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit... if you must, connect GND first.

void setup() {
  strip.begin();
  strip.show(); // initialize all pixels to "off"
}

void loop() {
  brighten();
  darken();
}

// 0 to 255
void brighten() {
  uint16_t i, j;

  for (j = 0; j < 255; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, 255);
    }
    strip.show();
    delay(10);
  }
  //delay(100);
}

// 255 to 0
void darken() {
  Serial.begin(9600);
  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, j);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(100);
}
void brighten()
{
  uint16_t i, j;
  for (j = 0; j < 255; j++)
  {
    for (i = 0; i < strip.numPixels(); i++)
    {
      strip.setPixelColor(i, 0, 0, 255);
    }
    strip.show();
    delay(10);
  }
  //delay(100);
}

Compare that with the darken() function and spot the difference in the parameters of the setPixelColor() function in the 2 functions

UKHeliBob:
Compare that with the darken() function and spot the difference in the parameters of the setPixelColor() function in the 2 functions

I got it, thank you so so much for your help!