Adafruit Neopixel code for simple brightness fade.

I'm new to arduino coding and i'm having A LOT of trouble coding this thing.

I'm using the Adafruit Neopixel Library and what I want to achieve is this:

I want to set my 12-light neopixel ring to one specific color (blue).

I want the ring to FADE from full brightness to half brightness with a roughly 2 second interval.

Thats it.

So far I can make it blink, chase, rainbow, etc. But not fade between brightness steps.

Any help would be greatly appreciated.

Okay I have this code 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(12, 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 = 45; j < 255; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j, j);
    }
    strip.show();
    delay(10);
  }
  //delay(1500);
}

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

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

And it fades exactly how I want only it's white. I've tried a few things to no avail. How to I make this set to 1 specific color?

1 Like

strip.setPixelColor(i, j, j, j);
change to
strip.setPixelColor(i, 0, 0, j);

Etc.

Thanks, that works great.

Next question, is there a way to add a little green to it? I've tried a few things with varying results.

strip.setPixelColor(i, 0, 10, j); // RGB

Thanks for your help man, I've really been struggling with this.

I tried that and it getting there but its not quite the effect I want. I'm want the green to fade in unison with the blue. Essentially making a single blue/green color pulsate up/down.

Hi Larryd, may I ask you how to set a Yellow color for the same sketch?
I changed it in "strip.setPixelColor(i,255,255,j);", but with maximum light the color gets back to white.

This is the full code:

#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(7, 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 = 10; j < 255; j++) {
for (i = 1; i <=strip.numPixels(); i++) {
strip.setPixelColor(i,255,255,j);
}
strip.show();
delay(10);
}
//delay(1500);
}

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

for (j = 255; j > 10; j--) {
for (i = 1; i <=strip.numPixels(); i++) {
strip.setPixelColor(i,255,255,j); // yellow with a little extra red to make it warmer. Instead of "strip.setPixelColor(i, 0, 0, j);"
}
strip.show();
delay(10);
Serial.println(j);
}
//delay(1500);
}

Sorry for disturb, thanks for your time.
Gio

PS: I set i=0 and numpixels=6 'cause I'm using an ardfruit jewel, and I wanna glow only the radial leds.

While I am at it, is there a way to have each of 4 neopixels fade at different times like have one pixel come on red and begin fading out and while that one is fading have another come on blue and start fading and then have the other 2 come on white then start fading. If not, i can handle them all fading at the same time, just curious.

The smart thing to do here (as always) is to start with a straight text description of what you want to achieve, then refine the statements into a series of operations. Each operation is then translated into a function or set of code statements, thus achieving the original goal. This is how any program or product of any kind is created.

By the way, I am at the moment working on a NeoPixel "Cylon Eye" (circa 70's Battlestar Galactica) which moves the "eye" focus up and down a NeoPixel string, fading the brightness in and out, and changing the color when the focus hits one end of the string and goes dark. Right now I'm working out the steps in brightness so that the intensity appears to change linearly. I've tried the log() and log10() functions to get better eyeball brightness linearity, but so far it's been a pretty miserable failure. I need to come up with something that matches the behavior of the LEDs to that of the viewer's perception.

You really shouldn't necro a 4 month old thread, but to answer your question, I wrote gamma code that works very well:

uint16_t LED_gamma(uint16_t intensity, uint16_t scale)
{
	uint32_t result = ((uint32_t)intensity * intensity * scale) >> 16;
	return (uint16_t)result;
}

It works because gamma correction is exponential, and a gamma of 2.0 corresponds to a simple square of the input quantity. The gamma of 2.0 may not be exact for the LEDs but it is close enough, and doesn't require any floating point calculations, especially transcendental functions.

The variable 'scale' is to allow fine tuning of the LED brightness, for example for compensating the different R,G,B values to obtain a chroma match because the R,G, and B diodes have different efficiencies. I actually use it to match discrete LEDs but it will work with Neo's too.

Really? A four-month-old thread is a corpse in your estimation? Particularly on a topic of such long-term interest? Yeeeeeah.

Anyway, thanks for the square idea. It's probby a little too extreme in changes and doesn't provide quite the linearity I was looking for. I found somebody else who had done a simple 32-step table of byte values for intensity. That's what I'm using now and it seems linear and clean. No math. If I need more resolution for a later project I'll just calculate the values to a larger table to get a smoother transition.

MartySchrader:
Anyway, thanks for the square idea. It's probby a little too extreme in changes and doesn't provide quite the linearity I was looking for

Does "probby" mean you didn't test it?

aarg:
Does "probby" mean you didn't test it?

Actually, I think I recall testing it and finding it too choppy. I made so many changes to this brightness thing in such a short time that it might have slipped through the cracks. My logbook doesn't show this particular solution being attempted, but a whole slew of other things I tried didn't get recorded there, either.

In any case, I liked the precalculated step solution the best and that's what I ended up with. A beneficial side effect is that the number of steps can be selected to exactly match some other factor in the fade mechanism, providing a sync of fade to LED position or some other factor. Works great.