// #include <Adafruit_NeoPixel.h>
// #define NUM_LEDS 16 // Sesuaikan jumlah LED yang Anda miliki
// #define DATA_PIN 3 // Pin data WS2812 terhubung ke pin D1 pada NodeMCU
// Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
// unsigned long previousMillis = 0;
// const long interval = 500; // Interval untuk mengubah warna
// int currentPixel = 0;
// int colorIndex = 0;
// const uint32_t colors[] = {
// strip.Color(255, 0, 0), // Merah
// strip.Color(255, 127, 0), // Jingga
// strip.Color(255, 255, 0), // Kuning
// strip.Color(0, 255, 0), // Hijau
// strip.Color(0, 255, 255), // Cyan
// strip.Color(0, 0, 255), // Biru
// strip.Color(75, 0, 130), // Ungu
// strip.Color(139, 0, 255) // Violet
// };
// void setup() {
// strip.begin();
// strip.show(); // Inisialisasi semua LED
// }
// void loop() {
// unsigned long currentMillis = millis();
// if (currentMillis - previousMillis >= interval) {
// previousMillis = currentMillis;
// // Reset semua LED
// for (int i = 0; i < NUM_LEDS; i++) {
// strip.setPixelColor(i, 0); // Matikan semua LED
// }
// // Set dua LED berikutnya dengan warna saat ini
// for (int i = 0; i < 1; i++) {
// int pixel = (currentPixel + i) % NUM_LEDS;
// strip.setPixelColor(pixel, colors[colorIndex]);
// }
// strip.show();
// // Perbarui indeks pixel dan warna
// currentPixel = (currentPixel + 1) % NUM_LEDS;
// colorIndex = (colorIndex + 1) % (sizeof(colors) / sizeof(colors[0]));
// }
// }
/////Pelangi berjalan////
#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 16 // Jumlah LED yang digunakan
#define DATA_PIN 3 // Pin data WS2812 terhubung ke pin D1 pada NodeMCU
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
unsigned long previousMillis = 0;
const long interval = 2; // Interval untuk mengubah warna
const uint32_t colors[] = {
strip.Color(255, 0, 0), // Merah
strip.Color(255, 127, 0), // Jingga
strip.Color(255, 255, 0), // Kuning
strip.Color(0, 255, 0), // Hijau
strip.Color(0, 255, 255), // Cyan
strip.Color(0, 0, 255), // Biru
strip.Color(75, 0, 130), // Ungu
strip.Color(139, 0, 255) // Violet
};
uint32_t ledColors[NUM_LEDS]; // Array untuk menyimpan warna setiap LED
////Gelap terang///
int brightness = 100; // Kecerahan awal
int fadeAmount = 5; // Jumlah perubahan kecerahan p
void setup() {
strip.begin();
strip.show(); // Inisialisasi semua LED
// Inisialisasi warna LED dengan dua set pelangi
for (int i = 0; i < NUM_LEDS; i++) {
ledColors[i] = colors[i % 8];
strip.setPixelColor(i, ledColors[i]);
}
strip.show();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Geser warna LED ke kanan
uint32_t lastColor = ledColors[NUM_LEDS - 1];
for (int i = NUM_LEDS - 1; i > 0; i--) {
ledColors[i] = ledColors[i - 1];
}
ledColors[0] = lastColor;
// // Set warna LED dengan array yang diperbarui
// for (int i = 0; i < NUM_LEDS; i++) {
// strip.setPixelColor(i, ledColors[i]);
// }
// strip.show();
////Gelap Terang///
// Perbarui kecerahan
brightness += fadeAmount;
if (brightness <= 100 || brightness >= 255) {
fadeAmount = -fadeAmount; // Balik arah fade
brightness = constrain(brightness, 0, 255); // Jaga agar kecerahan tetap dalam batas
}
// Set warna LED dengan array yang diperbarui dan kecerahan yang disesuaikan
for (int i = 0; i < NUM_LEDS; i++) {
uint32_t color = ledColors[i];
uint8_t red = (uint8_t)((color >> 16) & 0xFF);
uint8_t green = (uint8_t)((color >> 8) & 0xFF);
uint8_t blue = (uint8_t)(color & 0xFF);
strip.setPixelColor(i, strip.Color(
(red * brightness) / 255,
(green * brightness) / 255,
(blue * brightness) / 255
));
}
strip.show();
}
}