I am using a 24 led neopixel ring and I am having trouble figuring out the code. I want to be able to change a variable to choose the number of leds that are chasing around the ring, spaced evenly and looping. For example, if it is set to 1, then only one led is lit at a time going around the ring. if it is set to 2, the the first led and the 12th led are lit. both traveling in the same direction, and so on. I understand the code for one led but adding leds is where I'm stuck. I am also using the Adafruit_neopixel library.
Show the code you are using for more help. Also, look in your Neopixel library Examples folder for many example sketches.
#include <Adafruit_NeoPixel.h>
#define leds 24
#define PIN 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(leds, PIN, NEO_GRB + NEO_KHZ800);
int offset = 0;
int pix;
int speed = 100;
int i;
void setup()
{
strip.begin();
strip.setBrightness(10);
strip.clear();
Serial.begin(9600);
}
void loop()
{
uint32_t color = strip.Color(0,0,255);
for(i = 0; i < leds; i++) {
uint32_t c = 0;
if(((offset + i) & (leds / 4)) < 2) c = color;
strip.setPixelColor(i, c);
Serial.print("i: ");
Serial.println(i);
}
strip.show();
offset++;
delay(speed);
}
This line sets the "i" pixel to color "c".
The "n" pixel after "i" is "i+n".
Here is an example that has four pixels moving around a ring. One pixel a 0, one pixel at 1/4, one pixel at 1/2, one pixel at 3/4.
A very good library to use is FastLED... it has an example called Fade to Black. You will like it.
yes, that works for the most part. but when the led reaches the end it jumps to i+n and not 0.
Stop the loop at number_of_pixels minus "n"
thats closer but it still starts at i+n.
Yes it will do unless you do something in your code to stop this from happening.
One way of doing this it to use an if statement on the LED number you are about to set.if this number is greater than 19, then subtract 20 from the number your were trying to set.
Then when your offset reaches a certain value then reset the offset to zero.
I will leave it uo to you to work out what that certain number is.
Start counting at i-n
here is what i changed it to. it works better but "b" wont reach the end.
#include <Adafruit_NeoPixel.h>
#define leds 24
#define PIN 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(leds, PIN, NEO_GRB + NEO_KHZ800);
int offset = 0;
int pix;
int speed = 75;
int i;
int b;
int n = 2;
void setup()
{
strip.begin();
strip.setBrightness(10);
strip.clear();
Serial.begin(9600);
}
void loop()
{
uint32_t color = strip.Color(0,0,255);
uint32_t off = strip.Color(0,0,0);
for(i = 0; i < leds; i++) {
b = i - (leds/2);
strip.setPixelColor(i, color);
strip.setPixelColor(b, color);
strip.show();
strip.fill(off);
delay(speed);
Serial.println(i);
}
}
So you decided to ignore what I said then. Fine if you don't want help why ask for it.
You only need to count to half the ring. Start the second pixel half way.
Change these two lines.
for (i = 0; i < leds/2; i++) {
b = i + (leds / 2);
(it is quite possible Mike has me on "mute")
No loud and clear.
As he was addressing me in post #10 that I had assumed it was something to do with the method I told him about and not you.
that worked great thank you. however, is there a way to change a variable to choose how many leds are going at a time?
Maybe use a push-button or two?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.