#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Encoder enc(2, 3);
const int RED_PIN = 11;
const int GREEN_PIN = 6;
const int BLUE_PIN = 5;
const int BUTTON_PIN = 9;
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
int currentColor = 0; //
const int step = 15;
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);
updateRgb();
displayValues();
}
void loop() {
static int lastPosition = -1;
int position = enc.read() / step;
if (position != lastPosition) {
lastPosition = position;
changeColorValue(position);
updateRgb();
displayValues();
}
if (digitalRead(BUTTON_PIN) == LOW) {
currentColor = (currentColor + 1) % 3;
while(digitalRead(BUTTON_PIN) == LOW);
delay(50);
}
}
void changeColorValue(int position) {
if (currentColor == 0) {
redValue = constrain(position * step, 0, 255);
} else if (currentColor == 1) {
greenValue = constrain(position * step, 0, 255);
} else if (currentColor == 2) {
blueValue = constrain(position * step, 0, 255);
}
}
void updateRgb() {
analogWrite(RED_PIN, redValue);
analogWrite(GREEN_PIN, greenValue);
analogWrite(BLUE_PIN, blueValue);
}
void displayValues() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("R: "); lcd.print(redValue);
lcd.setCursor(1, 1);
lcd.print("G: "); lcd.print(greenValue);
lcd.setCursor(9, 1);
lcd.print("B: "); lcd.print(blueValue);
if (currentColor == 0) {
lcd.setCursor(0, 0);
lcd.print(">");
} else if (currentColor == 1) {
lcd.setCursor(0, 1);
lcd.print(">");
} else if (currentColor == 2) {
lcd.setCursor(8, 1);
lcd.print(">");
}
}