#include <Preferences.h>
#define RED_PIN 25
#define GREEN_PIN 26
#define BLUE_PIN 27
#define BTN_COLOR 14
#define BTN_UP 12
#define BTN_DOWN 13
#define PWM_FREQ 5000
#define PWM_RES 8
#define MAX_PWM 255
Preferences prefs;
int brightness = 128;
int colorIndex = 0;
unsigned long lastDebounce = 0;
int debounceDelay = 200;
int colors[][3] = {
{255, 0, 0}, // Kırmızı
{0, 255, 0}, // Yeşil
{0, 0, 255}, // Mavi
{255, 255, 0}, // Sarı
{0, 255, 255}, // Cyan
{255, 0, 255}, // Mor
{255, 255, 255} // Beyaz
};
void smoothWrite(int r, int g, int b) {
static int cr=0, cg=0, cb=0;
while (cr != r || cg != g || cb != b) {
if (cr < r) cr++;
if (cr > r) cr--;
if (cg < g) cg++;
if (cg > g) cg--;
if (cb < b) cb++;
if (cb > b) cb--;
ledcWrite(RED_PIN, cr * brightness / 255);
ledcWrite(GREEN_PIN, cg * brightness / 255);
ledcWrite(BLUE_PIN, cb * brightness / 255);
delay(5);
}
}
void saveSettings() {
prefs.begin("rgb", false);
prefs.putInt("bright", brightness);
prefs.putInt("color", colorIndex);
prefs.end();
}
void loadSettings() {
prefs.begin("rgb", true);
brightness = prefs.getInt("bright", 128);
colorIndex = prefs.getInt("color", 0);
prefs.end();
}
void setup() {
Serial.begin(115200);
ledcAttach(RED_PIN, PWM_FREQ, PWM_RES);
ledcAttach(GREEN_PIN, PWM_FREQ, PWM_RES);
ledcAttach(BLUE_PIN, PWM_FREQ, PWM_RES);
pinMode(BTN_COLOR, INPUT_PULLUP);
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
loadSettings();
Serial.println("RGB Sistem Hazır");
}
void loop() {
if (millis() - lastDebounce > debounceDelay) {
if (digitalRead(BTN_COLOR) == LOW) {
colorIndex++;
if (colorIndex > 6) colorIndex = 0;
saveSettings();
Serial.println("Renk Degisti");
lastDebounce = millis();
}
if (digitalRead(BTN_UP) == LOW) {
brightness += 15;
if (brightness > 255) brightness = 255;
saveSettings();
Serial.println("Parlaklik Artti");
lastDebounce = millis();
}
if (digitalRead(BTN_DOWN) == LOW) {
brightness -= 15;
if (brightness < 0) brightness = 0;
saveSettings();
Serial.println("Parlaklik Azaldi");
lastDebounce = millis();
}
}
smoothWrite(
colors[colorIndex][0],
colors[colorIndex][1],
colors[colorIndex][2]
);
}