#include <EEPROM.h>
// Пины
const byte RED_PIN = 13;
const byte GREEN_PIN = 12;
const byte BLUE_PIN = 11;
const byte BUTTON_PIN = 2;
// Настройки профилей (R, G, B)
const byte profiles[3][3] = {
{255, 0, 0}, // Профиль 0: Красный
{0, 255, 0}, // Профиль 1: Зелёный
{0, 0, 255} // Профиль 2: Синий
};
// Адрес в EEPROM для хранения текущего профиля
const int EEPROM_ADDR = 0;
byte currentProfile = 0;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 20; // ms
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Загрузка профиля из EEPROM
currentProfile = EEPROM.read(EEPROM_ADDR);
if (currentProfile >= 3) {
currentProfile = 0; // Защита от мусора в EEPROM
}
applyProfile(currentProfile);
}
void loop() {
static bool lastButtonState = HIGH;
bool buttonState = digitalRead(BUTTON_PIN);
// Проверка нажатия с антидребезгом
if (buttonState != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonState == LOW) { // Нажатие (активный LOW из-за pull-up)
currentProfile = (currentProfile + 1) % 3;
applyProfile(currentProfile);
EEPROM.write(EEPROM_ADDR, currentProfile); // Сохраняем в EEPROM
}
}
lastButtonState = buttonState;
}
void applyProfile(byte profileIndex) {
analogWrite(RED_PIN, profiles[profileIndex][0]);
analogWrite(GREEN_PIN, profiles[profileIndex][1]);
analogWrite(BLUE_PIN, profiles[profileIndex][2]);
}