Newbie here. I thought I had it figured out. I got some things to work. However not this.
Arduino Uno R3
The white light goes down the string and back just fine. But the blue does not light the whole string. It has strange behavior. It lights about 2/3 of the string and then some immediately go out so about half the string is lit. Then when the white starts again the blue at the end that wasn't lit lights up. Those blue then get over written by the white as expected. Why don't they all go blue? And why do some go blue but then go out right away?
I hope my code is clear enough.
Thanks
Lee
#include <Adafruit_NeoPixel.h>
#define PIN 2 // input pin Neopixel is attached to
#define NUMPIXELS 200 // number of neopixels in strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// Initialize the NeoPixel library.
pixels.begin();
// clear all
for (int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
}
} // end setup
void loop() {
// race forward
// white
for (int j=0; j<2; j++){
for (int i=0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(250, 250, 250));
pixels.setPixelColor(i-1, pixels.Color(0, 0, 0));
pixels.show();
}
// race back
for (int i = NUMPIXELS; i > 0; i = i-1)
{
pixels.setPixelColor(i+1, pixels.Color(0, 0, 0));
pixels.setPixelColor(i, pixels.Color(250, 250, 250));
pixels.show();
}
} // end race
// set all to blue
for (int i=0; i < NUMPIXELS; i++)
{
// green, red, blue
pixels.setPixelColor(i, pixels.Color(0, 0, 250));
pixels.show();
}// end blue
delay (500);
} // end loop
In both loops, you reach outside the range of i (see occurrences of i+1, i-1). Even if i is kept within the range 0...(Numpixels-1), those two excursions at the ends are writing to some other variable. Try:
for (int i = 1; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(250, 250, 250));
pixels.setPixelColor(i - 1, pixels.Color(0, 0, 0));
pixels.show();
}
// race back
for (int i = NUMPIXELS - 2; i >= 0; i--)
{
pixels.setPixelColor(i + 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(i, pixels.Color(250, 250, 250));
pixels.show();
}
I know it is outside the range. That was deliberate to clear the last light. There may be a better and more correct way but that part works. The blue loop that doesn't work I think is right.
I did some testing. It seems to depend on the color. If I change the blue section to white (250, 250, 250) it works. If I then change it to 200, 200, 250 it still works. But if I change it to 100, 100, 250 it does not work. What would the color have to do with it?
That loop writes past the end of the leds array. Maybe there is no variable at that memory address to corrupt.
edit:add
I see that you don't think that writing outside of the array that you declared is no biggie. Good luck puzzling over bugs in your code when you prioritize guesswork over unknowns.
Then put your thinking cap on, because you're not thinking straight. Look, use a length of 4 and actually work out the values you're indexing. With the code as you presented it, you go outside the length of the array twice. It's not up for debate. If you need to set endpoints with extra actions, do it outside the loop, because when you play with i+1 constructs, you can't let i be numLED or numLED-1, nor can you let i be 0 when you're accessing i-1. It's pretty simple.
Paul,
I did not understand your reference or realize that was code to change, as I am a Newbie. I did some research and changed it to NEO_KHZ400. That did not help.
GoForSmoke & Camsysca
I also changed it to NUMPIXELS-2 (and the other references) to stay in range.
I also removed the line
pixels.setPixelColor(i-1, pixels.Color(0, 0, 0))
and changed it to
pixels.clear ()
another thing I just learned. So now there shouldn't be any strange range reference.
That didn't help.
As I said it depends on the color. If I make it white it works but if I make it blue it does not work. This isn't making sense.
There's this thing about debugging. Where you see code that can cause a problem at all, you change that so it doesn't become a problem after you fix the one that bugs it up first.
You put an if() to see if the for-next index is at either end to not set values off the ends. Get it right once is have it right always.
Welcome to the forum..
You main issue, you go soOo fast..
and yes, should watch you indexes..
simmed your code here, threw a couple of evil delays in and looks like it works..
then i re-coded it using non-blocking delays..
I am having a problem getting a long string to stay in the right color.
I have a Uno R3. The 2811 are 12V and I have a good power supply. The Arduino is powered by the USB cable.
If I make all my LEDs white it works good. If I try to make them blue it does not. The last ones either don't light or have the wrong color. To be sure the code wasn't an issue, I downloaded the code from
and I am using the second pattern. All I changed was the pin number and the number of LEDs to fit my setup. I have 200 so I tried that. The last ones do not have the right color. So I changed it to 100 (still leaving the 200 connected) and it looks good. Then I tried 150 and some of them flash with the wrong color, but it is better than the 200.
I thought 200 should work. What is the limit and why does color make a difference. I figure white should take the most power and be a problem but I have the opposite.
With 12V strips the LEDs are addressed in groups of 3. That means if you have 150 the software should be written to address 50.
And it means the voltage & current is "shared" and you should estimate about 20mA per LED. For 200 LEDs, your power supply should be rated for about 4 Amps (or more).
And because the power conductors in the strip at "thin" you should also connect power about every meter to minimize voltage drop.
I don't have a strip. I can address each light individually. 50 and 150 get me two different bulbs. They are also connected with what looks like reasonable gauge wires.