When trying to help a user here on the forum, I came across a problem that I still haven't figured out if it's my error or the simulator's.
Ring LEDs (neopixel) are set to light blue,
and then there are 2 "for".
The first "for" turns the LEDs on completely and reduces the brightness until it goes out completely, and then the second "for" turns on the LEDs until it reaches its totality.
But when running in the simulator, only the first "for" works correctly, but the second "for" fails to gradually light up the LEDs.
Thanks for the help.
If I remember right, the setBrightness() function as currently implemented operates directly on the pixel array. So you loose the original information and can't really go back.
you would have to re-initialize the ring to full blue before dimming
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 16
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//-----------------------------------------------------------
void setup() {
Serial.begin(115200);
pixels.begin();
}
//-----------------------------------------------------------
void loop() {
for (int b = 255; b > 0; b -= 2) {
Serial.println(b);
for (int i = 0; i < NUMPIXELS; i++) pixels.setPixelColor(i, 0, 0, 255);
pixels.setBrightness(b);
pixels.show();
delay (20);
}
for (int b = 0; b < 255; b += 2) {
Serial.println(b);
for (int i = 0; i < NUMPIXELS; i++) pixels.setPixelColor(i, 0, 0, 255);
pixels.setBrightness(b);
pixels.show();
delay (20);
}
}
setBrightness() was intended to be called once, in setup(), to limit the current/brightness of the LEDs throughout the life of the sketch. It is not intended as an animation effect itself! The operation of this function is “lossy” — it modifies the current pixel data in RAM, not in the show() call — in order to meet NeoPixels’ strict timing requirements. Certain animation effects are better served by leaving the brightness setting at the default maximum, modulating pixel brightness in your own sketch logic and redrawing the full strip with setPixel().
Quoted from:
If I understand that correctly, it might work if you add this snippet from earlier in your code:
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, 0, 0, 255);
}
to set the pixel color of all the pixels after calling:
pixels.setBrightness(k);
but before:
pixels.show();
in your second loop. However, as the Adafruit comment points out, that is not the recommended way to do it as it is obviously inefficient so I'm not recommending it! I suppose another way might be to simply vary the intensity of the color from 0 to 255 and calling setPixel(). I wonder how others implement a brightness control?
Incidentally, you can shorten these slightly to:
for (int j = 255; j > 0; j = j - 2) {
for (int k = 0; k < 255; k = k + 2) {
to:
for (int j = 255; j > 0; j -= 2) {
for (int k = 0; k < 255; k += 2) {
When you are at zero brightness the underlying arrays goes to full black. 0 everywhere. Increasing 5he brightness of pitch black is still pitch black.
So the code will kinda work if you don’t make the value 0. Otherwise you are stuck
Note though that the brightness change you see is not compared to the original value, but the modified one. So it’s probably not exactly what you had in mind.
Otherwise You fill the array in with full blue but at current brightness of 0, so all the pixels are black. When you bump the brightness back to 255 afterwards it’s too late, they were pitch black and you can’t recover the blue because it got decimated with the 0 brightness
delay(500);
pixels.setBrightness(255);
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, 0, 0, 255);
}
pixels.show();
In a nutshell setPixelColor() takes the last set brightness into account so you don’t get the (0,0,255) you expected unless you are at full brightness