An Arduino novice here, I'm trying to create a short trailing pattern for long string of WS2812B LED's. Main purpose is for the trailing pattern to light up and travel at a defined speed "2 ft/s", trailing pattern to be 10 LED's and move across the length of the string. I've seen the sample codes from libraries like AdaFruit and FAST_LED yet, these light up the whole string as it moves forward, my intent is to only light up that section of 10 LED's and move them across.
Any hints as to how to do so? I'd imagine it's some kind of loop that specifies i from 0 to 10 then another loop to change the location of these, by either clearing out the path behind them.
I've been able to light up one LED at a time then looping it across the strip length, yet I'm missing how to make the code loop 10 LED's at a time and move them together.
PS: By now you may have also realized that I'm also a coding novice, by my choice of words, so if something here is not clear, please let me know and I'll rephrase as needed.
Here's the code I've used so far.
#include <Adafruit_NeoPixel.h>
#define PIN 2 // input pin Neopixel is attached to
#define NUMPIXELS 20 // number of neopixels in strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 250; // timing delay in milliseconds
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
void setup() {
// Initialize the NeoPixel library.
pixels.begin();
}
void loop() {
setColor();
for (int i=0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
// This sends the updated pixel color to the hardware.
pixels.show();
// Delay for a period of time (in milliseconds).
delay(delayval);
pixels.clear(); //This clears the pattern behind the one LED lighting up.
}
}
// setColor()
// picks random values to set for RGB
void setColor(){
redColor = random(0, 255);
greenColor = random(0,255);
blueColor = random(0, 255);
}
Since the library is NeoPixel, I guess this is ok because .setPixelColor() will probably ignore any pixel number which is greater or equal to NUMPIXELS.
But if @ashgo1094 was using FastLed library, this would be a problem because with that library, your code accesses the pixel array directly, and with the above calculation of the pixel i + u would exceed the boundary of the array (ie. exceed NUMPIXELS-1) and cause some strange effects!
A solution that would work for either library could be
for (byte u = i; u < i + 10 && u < NUMPIXELS; u++) {
pixels.setPixelColor(u, pixels.Color(redColor, greenColor, blueColor));
}
I have a similar project, yet I'm trying to have two speeds available, and toggling them through a button, how could I adapt this code to do so?
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#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)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
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() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.begin();
strip.setBrightness(50);
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Some example procedures showing how to display to the pixels:
colorWipe(strip.Color(255, 0, 0), 50); // Red
// colorWipe(strip.Color(0, 255, 0), 50); // Green
// colorWipe(strip.Color(0, 0, 255), 50); // Blue
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(23);
}
}