#include <Adafruit_NeoPixel.h>
#define PIN_LED 5
#define WIDTH 8
#define HEIGHT 8
#define NUM_LEDS (WIDTH * HEIGHT)
Adafruit_NeoPixel strip(NUM_LEDS, PIN_LED, NEO_GRB + NEO_KHZ800);
unsigned long lastUpdate = 0;
const unsigned long animInterval = 50; // kecepatan animasi
int frame = 0;
int spotlightPos = 0;
void setup() {
strip.begin();
strip.show();
}
void loop() {
unsigned long now = millis();
if (now - lastUpdate >= animInterval) {
rainbowWave(frame); // background pelangi
//chaseSpotlight(spotlightPos); // spotlight putih
chaseSpotlightTrail(spotlightPos); // Ada Kometnya
frame++;
spotlightPos = (spotlightPos + 1) % NUM_LEDS; // bergerak terus
lastUpdate = now;
}
}
// Background rainbow wave
void rainbowWave(int frame) {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int index = xyToIndex(x, y);
uint16_t hue = (x * 32 + frame * 8) % 256;
uint32_t color = strip.ColorHSV(hue * 256, 255, 150); // pelangi agak redup
strip.setPixelColor(index, color);
}
}
}
// Spotlight putih bergerak
void chaseSpotlight(int pos) {
strip.setPixelColor(pos, strip.Color(255, 255, 255)); // putih terang
strip.show();
}
// Mapping XY ke index linear (zig-zag wiring)
int xyToIndex(int x, int y) {
if (y % 2 == 0) {
return y * WIDTH + x;
} else {
return y * WIDTH + (WIDTH - 1 - x);
}
}
void chaseSpotlightTrail(int pos) {
// panjang ekor
int trailLength = 4;
for (int i = 0; i < trailLength; i++) {
int index = (pos - i + NUM_LEDS) % NUM_LEDS;
int brightness = 255 - i*60; // makin redup ke belakang
strip.setPixelColor(index, strip.Color(brightness, brightness, brightness));
}
strip.show();
}