// Define pin assignments
const int LED_PIN = 9;
const int BTN1_PIN = 2;
const int BTN2_PIN = 3;
const int POT1_PIN = A0;
const int POT2_PIN = A1;
const int POT3_PIN = A2;

// Define debounce delay
const unsigned long DEBOUNCE_DELAY = 150;

// Define state persistence delay
const unsigned long STATE_PERSISTENCE_DELAY = 2000;

// Define transition time for Auto Mode
const unsigned long TRANSITION_TIME = 2000;

// Variables to track the current state
bool isManualMode = true;
unsigned long lastStateChangeTime = 0;

// Variables for button debouncing
bool btn1LastState = HIGH;
bool btn2LastState = HIGH;
unsigned long lastBtn1DebounceTime = 0;
unsigned long lastBtn2DebounceTime = 0;

// Variables for Auto Mode
unsigned long lastTransitionTime = 0;
bool isIncreasing = true;
int currentBrightness = 0;

void setup() {
  // Initialize pins
  pinMode(LED_PIN, OUTPUT);
  pinMode(BTN1_PIN, INPUT_PULLUP);
  pinMode(BTN2_PIN, INPUT_PULLUP);

  // Initialize the LED to off at startup
  analogWrite(LED_PIN, 0);
}

void loop() {
  unsigned long currentTime = millis();

  // Read button states
  bool btn1Reading = digitalRead(BTN1_PIN);
  bool btn2Reading = digitalRead(BTN2_PIN);

  // Handle button 1 debouncing
  if (btn1Reading != btn1LastState) {
    lastBtn1DebounceTime = currentTime;
  }

  // Handle button 2 debouncing
  if (btn2Reading != btn2LastState) {
    lastBtn2DebounceTime = currentTime;
  }

  // Check if a debounced button 1 press has occurred
  if ((currentTime - lastBtn1DebounceTime) > DEBOUNCE_DELAY) {
    if (btn1Reading == LOW && btn1LastState == HIGH) {
      // Button 1 has been pressed
      if (isManualMode && (currentTime - lastStateChangeTime) > STATE_PERSISTENCE_DELAY) {
        // Switch to Auto Mode
        isManualMode = false;
        lastStateChangeTime = currentTime;
        lastTransitionTime = currentTime;
        isIncreasing = true;
      }
    }
  }

  // Check if a debounced button 2 press has occurred
  if ((currentTime - lastBtn2DebounceTime) > DEBOUNCE_DELAY) {
    if (btn2Reading == LOW && btn2LastState == HIGH) {
      // Button 2 has been pressed
      if (!isManualMode && (currentTime - lastStateChangeTime) > STATE_PERSISTENCE_DELAY) {
        // Switch to Manual Mode
        isManualMode = true;
        lastStateChangeTime = currentTime;
      }
    }
  }

  // Update button states
  btn1LastState = btn1Reading;
  btn2LastState = btn2Reading;

  // Handle the current mode
  if (isManualMode) {
    // In Manual Mode, control the LED brightness with pot1
    int pot1Value = analogRead(POT1_PIN);
    int brightness = map(pot1Value, 0, 1023, 0, 255);
    analogWrite(LED_PIN, brightness);
  } else {
    // In Auto Mode, cycle the LED brightness between min and max values

    // Read the min and max brightness from pot2 and pot3
    int minBrightness = map(analogRead(POT2_PIN), 0, 1023, 0, 255);
    int maxBrightness = map(analogRead(POT3_PIN), 0, 1023, 0, 255);

    // Ensure minBrightness is not greater than maxBrightness
    if (minBrightness > maxBrightness) {
      int temp = minBrightness;
      minBrightness = maxBrightness;
      maxBrightness = temp;
    }

    // Calculate the time elapsed since the last transition
    unsigned long elapsedTime = currentTime - lastTransitionTime;

    // Calculate the new brightness based on the elapsed time
    if (isIncreasing) {
      // Increasing brightness
      currentBrightness = map(elapsedTime, 0, TRANSITION_TIME, minBrightness, maxBrightness);

      if (elapsedTime >= TRANSITION_TIME) {
        isIncreasing = false;
        lastTransitionTime = currentTime;
      }
    } else {
      // Decreasing brightness
      currentBrightness = map(elapsedTime, 0, TRANSITION_TIME, maxBrightness, minBrightness);

      if (elapsedTime >= TRANSITION_TIME) {
        isIncreasing = true;
        lastTransitionTime = currentTime;
      }
    }

    // Apply the brightness to the LED
    analogWrite(LED_PIN, currentBrightness);
  }
}
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
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
pot1:VCC
pot1:SIG
pot1:GND
pot2:VCC
pot2:SIG
pot2:GND
pot3:VCC
pot3:SIG
pot3:GND