#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Pin definitions
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
const int POT_PIN = A0;
const int BTN_RED = 2;
const int BTN_GREEN = 3;
const int BTN_BLUE = 4;

// LCD setup (adjust address if needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Variables
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
int currentColor = 0; // 0: Red, 1: Green, 2: Blue

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(BTN_RED, INPUT_PULLUP);
  pinMode(BTN_GREEN, INPUT_PULLUP);
  pinMode(BTN_BLUE, INPUT_PULLUP);

  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("R:   G:   B:");
}

void loop() {
  // Read buttons
  if (digitalRead(BTN_RED) == LOW) currentColor = 0;
  if (digitalRead(BTN_GREEN) == LOW) currentColor = 1;
  if (digitalRead(BTN_BLUE) == LOW) currentColor = 2;

  // Read potentiometer
  int potValue = analogRead(POT_PIN);
  int brightness = map(potValue, 0, 1023, 0, 255);

  // Update color values
  switch (currentColor) {
    case 0: redValue = brightness; break;
    case 1: greenValue = brightness; break;
    case 2: blueValue = brightness; break;
  }

  // Update LED
  analogWrite(RED_PIN, redValue);
  analogWrite(GREEN_PIN, greenValue);
  analogWrite(BLUE_PIN, blueValue);

  // Update LCD
  lcd.setCursor(2, 1);
  lcd.print("   ");
  lcd.setCursor(2, 1);
  lcd.print(redValue);

  lcd.setCursor(7, 1);
  lcd.print("   ");
  lcd.setCursor(7, 1);
  lcd.print(greenValue);

  lcd.setCursor(12, 1);
  lcd.print("   ");
  lcd.setCursor(12, 1);
  lcd.print(blueValue);

  delay(50); // Small delay for stability
}