// Pin assignments
const int led1Pin = 2;  // PWM pin for LED 1
const int led2Pin = 3;  // PWM pin for LED 2
const int led3Pin = 4;  // PWM pin for LED 3

const int btn1Pin = 5;  // Digital pin for button 1
const int btn2Pin = 6;  // Digital pin for button 2
const int btn3Pin = 7;  // Digital pin for button 3

const int pot1Pin = A0;  // Analog pin for potentiometer 1
const int pot2Pin = A1;  // Analog pin for potentiometer 2
const int pot3Pin = A2;  // Analog pin for potentiometer 3

// Button state variables
int btn1State = HIGH;
int btn2State = HIGH;
int btn3State = HIGH;

// Previous button states for debouncing
int lastBtn1State = HIGH;
int lastBtn2State = HIGH;
int lastBtn3State = HIGH;

// Timing variables
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long lastBtn1PressTime = 0;
unsigned long lastBtn2PressTime = 0;
unsigned long lastBtn3PressTime = 0;

// Constants
const int debounceDelay = 50;      // Debounce time in milliseconds
const int stabilityDelay = 2000;   // 2 second stability period

// LED brightness variables
int led1Brightness = 0;
int led2Brightness = 0;
int led3Brightness = 0;

void setup() {
  // Initialize LED pins as outputs
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  
  // Initialize button pins with internal pull-up resistors
  pinMode(btn1Pin, INPUT_PULLUP);
  pinMode(btn2Pin, INPUT_PULLUP);
  pinMode(btn3Pin, INPUT_PULLUP);
  
  // Set all LEDs off initially
  analogWrite(led1Pin, 0);
  analogWrite(led2Pin, 0);
  analogWrite(led3Pin, 0);
}

void loop() {
  // Read current button states
  int reading1 = digitalRead(btn1Pin);
  int reading2 = digitalRead(btn2Pin);
  int reading3 = digitalRead(btn3Pin);
  
  // Get current time
  unsigned long currentTime = millis();

  // Handle Button 1
  if (reading1 != lastBtn1State) {
    lastDebounceTime1 = currentTime;
  }
  
  if ((currentTime - lastDebounceTime1) > debounceDelay) {
    if (reading1 != btn1State) {
      btn1State = reading1;
      
      // Button 1 is pressed (LOW state)
      if (btn1State == LOW) {
        // Only update LED if at least 2 seconds have passed since last update
        if (currentTime - lastBtn1PressTime > stabilityDelay) {
          // Read potentiometer value and map to LED brightness
          int potValue = analogRead(pot1Pin);
          led1Brightness = map(potValue, 0, 1023, 0, 255);
          
          // Update LED brightness
          analogWrite(led1Pin, led1Brightness);
          
          // Record time of button press
          lastBtn1PressTime = currentTime;
        }
      }
    }
  }
  
  // Handle Button 2
  if (reading2 != lastBtn2State) {
    lastDebounceTime2 = currentTime;
  }
  
  if ((currentTime - lastDebounceTime2) > debounceDelay) {
    if (reading2 != btn2State) {
      btn2State = reading2;
      
      // Button 2 is pressed (LOW state)
      if (btn2State == LOW) {
        // Only update LED if at least 2 seconds have passed since last update
        if (currentTime - lastBtn2PressTime > stabilityDelay) {
          // Read potentiometer value and map to LED brightness
          int potValue = analogRead(pot2Pin);
          led2Brightness = map(potValue, 0, 1023, 0, 255);
          
          // Update LED brightness
          analogWrite(led2Pin, led2Brightness);
          
          // Record time of button press
          lastBtn2PressTime = currentTime;
        }
      }
    }
  }
  
  // Handle Button 3
  if (reading3 != lastBtn3State) {
    lastDebounceTime3 = currentTime;
  }
  
  if ((currentTime - lastDebounceTime3) > debounceDelay) {
    if (reading3 != btn3State) {
      btn3State = reading3;
      
      // Button 3 is pressed (LOW state)
      if (btn3State == LOW) {
        // Only update LED if at least 2 seconds have passed since last update
        if (currentTime - lastBtn3PressTime > stabilityDelay) {
          // Read potentiometer value and map to LED brightness
          int potValue = analogRead(pot3Pin);
          led3Brightness = map(potValue, 0, 1023, 0, 255);
          
          // Update LED brightness
          analogWrite(led3Pin, led3Brightness);
          
          // Record time of button press
          lastBtn3PressTime = currentTime;
        }
      }
    }
  }
  
  // Update previous button states for debouncing
  lastBtn1State = reading1;
  lastBtn2State = reading2;
  lastBtn3State = reading3;
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
pot1:VCC
pot1:SIG
pot1:GND
pot2:VCC
pot2:SIG
pot2:GND
pot3:VCC
pot3:SIG
pot3:GND