#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define RED_PIN 6
#define GREEN_PIN 5
#define BLUE_PIN 4
#define ENCODER_A 2
#define ENCODER_B 3
#define BUTTON_PIN 8
volatile int encoderPos = 0;
int currentColor = 0; // 0 - красный, 1 - зеленый, 2 - синий
int colorValues[3] = {0, 0, 0};
void setup() {
lcd.begin(16, 2);
lcd.backlight();
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(ENCODER_A, INPUT);
pinMode(ENCODER_B, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_A), updateEncoder, CHANGE);
updateDisplay();
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
delay(200); // Дебаунс кнопки
currentColor = (currentColor + 1) % 3; // Переключение между R, G и B
updateDisplay();
while (digitalRead(BUTTON_PIN) == LOW); // Ожидание отпускания кнопки
}
// Изменение значения цвета при вращении энкодера
if (encoderPos != 0) {
if (currentColor == 0) { // Красный
colorValues[0] = constrain(colorValues[0] + (encoderPos > 0 ? 15 : -15), 0, 255);
} else if (currentColor == 1) { // Зеленый
colorValues[1] = constrain(colorValues[1] + (encoderPos > 0 ? 15 : -15), 0, 255);
} else if (currentColor == 2) { // Синий
colorValues[2] = constrain(colorValues[2] + (encoderPos > 0 ? 15 : -15), 0, 255);
}
setColor(colorValues[0], colorValues[1], colorValues[2]);
encoderPos = 0; // Сбрасываем позицию энкодера после изменения цвета
updateDisplay();
}
}
void updateEncoder() {
int stateA = digitalRead(ENCODER_A);
int stateB = digitalRead(ENCODER_B);
if (stateA == stateB) {
encoderPos = 1; // Вращение по часовой стрелке
} else {
encoderPos = -1; // Вращение против часовой стрелки
}
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
// Отображаем значения цветов
lcd.print("R:");
lcd.print(colorValues[0]);
lcd.print(" G:");
lcd.print(colorValues[1]);
lcd.setCursor(0, 1);
lcd.print("B:");
lcd.print(colorValues[2]);
}
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}