#include <EEPROM.h>
#include "Encoder.h"
#include "Button.h"
#include "SleepMode.h"
#define LED1_PIN 0
#define LED2_PIN 1
#define MAX_BRIGHTNESS 255
#define MIN_BRIGHTNESS 10
enum Mode {LED1, LED2, BOTH};
Mode currentMode = LED1;
uint8_t brightness1;
uint8_t brightness2;
uint8_t currentBrightness1;
uint8_t currentBrightness2;
void setup() {
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
buttonSetup();
encoderSetup();
// Разрешаем прерывание по кнопке
GIMSK |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT2);
// Читаем сохранённые значения яркости из EEPROM
currentBrightness1 = EEPROM.read(0);
currentBrightness2 = EEPROM.read(1);
}
void loop() {
if (isButtonHeldFor(3000)) {
enterSleep();
while (!isButtonHeldFor(3000));
exitSleep();
}
if (isButtonPressed()) {
switch (currentMode) {
case LED1:
currentMode = LED2;
break;
case LED2:
currentMode = BOTH;
break;
case BOTH:
currentMode = LED1;
break;
}
delay(200); // Debounce delay
}
int encoderValue = readEncoder();
if (encoderValue != 0) {
switch (currentMode) {
case LED1:
currentBrightness1 = constrain(currentBrightness1 + encoderValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
EEPROM.update(0, currentBrightness1);
break;
case LED2:
currentBrightness2 = constrain(currentBrightness2 + encoderValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
EEPROM.update(1, currentBrightness2);
break;
case BOTH:
currentBrightness1 = constrain(currentBrightness1 + encoderValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
currentBrightness2 = constrain(currentBrightness2 + encoderValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
EEPROM.update(0, currentBrightness1);
EEPROM.update(1, currentBrightness2);
break;
}
}
// Устанавливаем яркость светодиодов
analogWrite(LED1_PIN, (currentMode == LED1 || currentMode == BOTH) ? currentBrightness1 : 0);
analogWrite(LED2_PIN, (currentMode == LED2 || currentMode == BOTH) ? currentBrightness2 : 0);
delay(100);
}
ISR(PCINT0_vect) {
exitSleep();
}
void enterSleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_bod_disable();
sei(); // Включаем прерывания
sleep_cpu();
}
void exitSleep() {
cli(); // Отключаем прерывания
sleep_disable();
sei(); // Включаем прерывания
}