#include <Adafruit_NeoPixel.h>
const byte brocheStrip = 5;
const uint16_t nbLeds = 200;
Adafruit_NeoPixel strip(nbLeds, brocheStrip, NEO_GRB + NEO_KHZ800);
const byte nbPistes = 4;
uint16_t position[nbPistes];
const byte longueurTraine = 5;
unsigned long deltaT = 30; // nombre de ms entre 2 décalages
uint16_t teintePiste[nbPistes];
void affichePistes() {
static unsigned long dernierMillis = -deltaT;
if (millis() - dernierMillis >= deltaT) {
for (byte piste = 0; piste < nbPistes; piste++) {
strip.setPixelColor(position[piste], Adafruit_NeoPixel::ColorHSV(teintePiste[piste], 255, 255)); // on affiche le point de tête
// on dessine la traîne
for (uint16_t i = 1; i <= longueurTraine; i++) {
uint16_t pos = (position[piste] + nbLeds - i) % nbLeds;
strip.setPixelColor(pos, Adafruit_NeoPixel::ColorHSV(teintePiste[piste], 255, map(i, -1, longueurTraine, 255, 0))); // on affiche le point de tête
}
position[piste] = (position[piste] + 1) % nbLeds; // on avance d'un pixel pour la prochaine fois
}
strip.show();
for (uint16_t i = 0; i < nbLeds; i++) strip.setPixelColor(i, 0); // on pré-efface pour la prochaine fois
dernierMillis = millis();
}
}
void setup() {
for (byte piste = 0; piste < nbPistes; piste++) {
position[piste] = piste * (nbLeds / nbPistes); // positions initiales
teintePiste[piste] = piste * (0xFFFFu / nbPistes); // répartition de différentes teintes
}
Serial.begin(115200);
strip.begin();
}
void loop() {
affichePistes();
}