#include <LiquidCrystal_I2C.h>
// Common (-) LED pins
const byte ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const byte ledCount = sizeof ledPins / sizeof ledPins[0];
// RGB + pins
const byte RED_PIN = 12;
const byte GREEN_PIN = 13;
const byte BLUE_PIN = A0;
// Pot Pins
const byte RGB_SPEED_POT = A2; // RGB Color Speed
const byte LED_SPEED_POT = A3; // LED Steping speed
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
for (byte i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
// lcd.print(" LED RGB");
lcd.print(" LED ");
lcd.print(" RGB");
}
void loop() {
static byte ledIndex = 0;
static byte colorIndex = 0;
static uint16_t prevLedInterval = 0;
static uint16_t prevRgbInterval = 0;
uint32_t now = millis();
static uint32_t nextLedMillis = 0;
static uint32_t nextRgbMillis = 0;
// --- Pots Readings ---
uint16_t ledInterval = map(analogRead(LED_SPEED_POT), 0, 1023, 50, 999);
uint16_t rgbInterval = map(analogRead(RGB_SPEED_POT), 0, 1023, 50, 999);
lcd.setCursor(3, 1);
if(ledInterval < 100) lcd.print(" ");
lcd.print(ledInterval);
lcd.setCursor(10, 1);
if(rgbInterval < 100) lcd.print(" ");
lcd.print(rgbInterval);
// ---------- LED Interval ----------
if (now >= nextLedMillis) {
nextLedMillis += ledInterval;
// Previous LED Off
digitalWrite(ledPins[ledIndex], LOW);
// Next LED
ledIndex++;
if (ledIndex >= ledCount) ledIndex = 0;
digitalWrite(ledPins[ledIndex], HIGH);
}
// ---------- RGB Color Gradient ----------
if (now >= nextRgbMillis) {
nextRgbMillis += rgbInterval;
colorIndex++;
if (colorIndex > 0b0111) colorIndex = 1;
digitalWrite(RED_PIN, !(colorIndex & 1));
digitalWrite(GREEN_PIN, !(colorIndex & 2));
digitalWrite(BLUE_PIN, !(colorIndex & 4));
}
}