#include <Adafruit_NeoPixel.h>
// Define the pin for the NeoPixel data input
#define PIN 6
// Define the number of pixels in the NeoPixel Ring
#define NUMPIXELS 24
// Create an instance of the Adafruit_NeoPixel class
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Run the chaser effect to and fro
chaserEffect();
}
void chaserEffect() {
// Define the delay between updates
int delayTime = 50;
// Move the chaser from the first pixel to the last
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, Wheel((i * 256 / NUMPIXELS) & 255));
if (i > 0) {
strip.setPixelColor(i - 1, 0); // Turn off the previous pixel
}
strip.show();
delay(delayTime);
}
// Move the chaser from the last pixel to the first
for (int i = NUMPIXELS - 1; i >= 0; i--) {
strip.setPixelColor(i, Wheel((i * 256 / NUMPIXELS) & 255));
if (i < NUMPIXELS - 1) {
strip.setPixelColor(i + 1, 0); // Turn off the previous pixel
}
strip.show();
delay(delayTime);
}
}
// Input a value 0 to 255 to get a color value.
// The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}