// -----------------------------Include libraries -------------------
#include <Arduino.h>

// -----------------------------Define variables----------------------
int debounceDelay = 50;
int minLEDdim = 10; // minimum LED dim level (never let the pots turn the LEDs off completely)

// -----------------------------Define structures---------------------
struct Switch {
  String name;
  int pin1;
  //  int pin2;  // for SP3T, 0 for SPST (pin 2 is always gnd - no definition req'd)
  int pin3;  // for SP3T, 0 for SPST
  int state1; // the states match the LED structure for easy matching
  int state2; // this will be set to high if state 1 and 3 are low 
  int state3; // this should initialize at low and stay low for SPST switches (which don't have a third state)
};

struct LEDSet {
  String name;
  int dimPin; // for sets of leds that only have one, only the dim pin and dim level is used
  int pin1; // Used for 2 or 3 LEDs, 0 if 1 LED
  int pin2; // Used for 2 or 3 LEDs, 0 if 1 LED
  int pin3;  // Used for 3 LEDs, 0 otherwise
  int state1;
  int state2; 
  int state3;
  int dimLevel;
};

struct Button {
  String name;
  int pin;
  int state;
  int lastState;
  unsigned long lastDebounceTime;  // add debounce time
  //int debounceDelay;  // the debounce time; increase if the output flickers
};

struct Potentiometer {
  String name;
  int pin;
  int state; //this will utilize the analog in values from 0-1024
};

struct SimonLED {
  String name;
  int dimPin;
  int dimLevel;
  unsigned long turnOffTime;
};

// ----------------------instances of structures----------------------------
// Switch instances (set here is teh set of data for one switch)
// struct: name, pin 1, pin 3, State 1, State 2, State 3)
Switch SWset1 = {"PowerSwitch1", A13, 0, LOW, LOW, LOW};
Switch SWset2 = {"PowerSwitch2", A14, 0, LOW, LOW, LOW};
Switch SWset3 = {"SelectionSwitch1", A12, 0, LOW, LOW, LOW};
Switch SWset4 = {"SelectionSwitch2", A15, 0, LOW, LOW, LOW};
Switch SWset5 = {"SP3T1", 22, 23, LOW, LOW, LOW};
Switch SWset6 = {"SP3T2", 24, 25, LOW, LOW, LOW};
Switch SWset7 = {"SP3T3", 26, 27, LOW, LOW, LOW};
Switch SWset8 = {"SP3T4", 28, 29, LOW, LOW, LOW};
Switch SWset9 = {"SPST1", A10, 0, LOW, LOW, LOW};
Switch SWset10 = {"SPST2", A11, 0, LOW, LOW, LOW};

// LED instances (set here is up to 3 LEDs and associated with the same switch number)
// struct: name, dim pin, pin 1, pin 2, pin 3, State 1, State 2, State 3, dimlevel)
LEDSet LEDset1 = {"PowerLED1", 6, 0, 0, 0, HIGH, HIGH, HIGH, 128};
LEDSet LEDset2 = {"PowerLED2", 7, 0, 0, 0, HIGH, HIGH, HIGH, 128};
LEDSet LEDset3 = {"null", 0, 0, 0, 0, HIGH, HIGH, HIGH, 0};
LEDSet LEDset4 = {"null", 0, 0, 0, 0, HIGH, HIGH, HIGH, 0};
LEDSet LEDset5 = {"SP3TSetLED1", 2, 30, 31, 32, HIGH, HIGH, HIGH, 128};
LEDSet LEDset6 = {"SP3TSetLED2", 3, 33, 34, 35, HIGH, HIGH, HIGH, 128};
LEDSet LEDset7 = {"SP3TSetLED3", 4, 36, 37, 38, HIGH, HIGH, HIGH, 128};
LEDSet LEDset8 = {"SP3TSetLED4", 5, 39, 40, 41, HIGH, HIGH, HIGH, 128};
LEDSet LEDset9 = {"SPSTSetLED1", 44, 43, 42, 0, HIGH, HIGH, HIGH, 128};
LEDSet LEDset10 = {"SPSTSetLED2", 45, 49, 48, 0, HIGH, HIGH, HIGH, 128};

// Potentiometer instances
Potentiometer Pot1 = {"Potentiometer1", A0, 0};
Potentiometer Pot2 = {"Potentiometer2", A1, 0};
Potentiometer Pot3 = {"Potentiometer3", A2, 0};

// Create instances of the Buttons
Button ButtonDown = {"Down", 50, HIGH, HIGH, 0};
Button ButtonUp = {"Up", 51, HIGH, HIGH, 0};
Button ButtonLeft = {"Left", 52, HIGH, HIGH, 0};
Button ButtonRight = {"Right", 53, HIGH, HIGH, 0};

// Create instances of the simon says LEDs
SimonLED SimonLEDLeft = {"Left", 8, 0, 0};
SimonLED SimonLEDRight = {"Right", 9, 0, 0};
SimonLED SimonLEDUp = {"Up", 10, 0, 0};
SimonLED SimonLEDDown = {"Down", 11, 0, 0};

//------------------------------Arrays of Structures-----------------------
//This could be combined with the above, if the code doesn't need to individually
//call the above structure instances, but I'm keeping them separate for the moment.

// Array of all potentiometers
Potentiometer allPots[] = {Pot1, Pot2, Pot3};

// Array of all switch sets
Switch allSwitches[] = {SWset1, SWset2, SWset3, SWset4, SWset5, SWset6, SWset7, SWset8, SWset9, SWset10};

// Array of all LED sets
LEDSet allLEDs[] = {LEDset1, LEDset2, LEDset3, LEDset4, LEDset5, LEDset6, LEDset7, LEDset8, LEDset9, LEDset10}; // Add LEDset9 and LEDset10

// Array of all buttons
Button allButtons[] = {ButtonDown, ButtonUp, ButtonLeft, ButtonRight};

// Array of simon LEDs
SimonLED simonLEDs[] = {SimonLEDDown, SimonLEDUp, SimonLEDLeft, SimonLEDRight};


//--------------------------------Functions --------------------------------
void checkSwitchStates() {
  for (int i = 0; i < sizeof(allSwitches) / sizeof(Switch); i++) {
    // Check the state of pin 1
    if (digitalRead(allSwitches[i].pin1) == LOW) { //switches are to ground with input pullup. Low means switch is "on" or "up".
      allSwitches[i].state1 = LOW; 
    } else {
      allSwitches[i].state1 = HIGH;
    }
    
    // If pin 3 exists, check its state. This only for SP3T switches.
    if (allSwitches[i].pin3 != 0) {
      if (digitalRead(allSwitches[i].pin3) == LOW) { //Low here means SP3T switch is "Down"
        allSwitches[i].state3 = LOW;
      } else {
        allSwitches[i].state3 = HIGH;
      }
    } else {
      allSwitches[i].state3 = HIGH; //if pin 3 is 0 then it's a SPST switch and we keep state 3 High (off)
    }

    // Set state2 LOW if states 1 and 3 are HIGH (off) 
    if (allSwitches[i].state1 == HIGH && allSwitches[i].state3 == HIGH) {
      allSwitches[i].state2 = LOW;
    } else {
      allSwitches[i].state2 = HIGH;
    }
  }
}

void matchLEDStates() {
  for(int i = 0; i < sizeof(allSwitches) / sizeof(Switch); i++) {
    if (allLEDs[i].name != "null") {
      allLEDs[i].state1 = allSwitches[i].state1;
      allLEDs[i].state2 = allSwitches[i].state2;
      allLEDs[i].state3 = allSwitches[i].state3;
    }
  }
}

void updateLEDs() {
  for(int i = 0; i < sizeof(allLEDs) / sizeof(LEDSet); i++) {
    if (allLEDs[i].name != "null") {
      // If the LED set only contains one LED and no control pins,
      // turn it off when state1 is HIGH by setting analogWrite to 0.
      if(allLEDs[i].pin1 == 0 && allLEDs[i].pin2 == 0 && allLEDs[i].pin3 == 0 && allLEDs[i].state1 == HIGH) {
        analogWrite(allLEDs[i].dimPin, 0);

      } else {   

        // Turn on or off each LED based on its state by matching the switch state (LOW is ON)
        if(allLEDs[i].pin1 != 0) {
          digitalWrite(allLEDs[i].pin1, allLEDs[i].state1); 
        }
        if(allLEDs[i].pin2 != 0) {
          digitalWrite(allLEDs[i].pin2, allLEDs[i].state2); 
        }
        if(allLEDs[i].pin3 != 0) {
          digitalWrite(allLEDs[i].pin3, allLEDs[i].state3); 
        }
        
        // Update the brightness level
        analogWrite(allLEDs[i].dimPin, allLEDs[i].dimLevel);
      }
    }
  }
}

void checkPotentiometers() {
  for(int i = 0; i < sizeof(allPots) / sizeof(Potentiometer); i++) {
    // Read the value of the potentiometer (from 0 to 1023)
    allPots[i].state = analogRead(allPots[i].pin);

    // Scale the value down to between 0 and 255
    allPots[i].state = map(allPots[i].state, 0, 1023, 0, 255);

    // Print the potentiometer state to the serial monitor
    //Serial.print(allPots[i].name);
    //Serial.print(": ");
    //Serial.println(allPots[i].state);
  }
}

void setLEDDimming() {
  for(int i = 0; i < sizeof(allLEDs) / sizeof(LEDSet); i++) {
    if (allLEDs[i].name != "null") {
      // Use the state of the corresponding potentiometer to set the dimming level
      allLEDs[i].dimLevel = max(allPots[i % (sizeof(allPots) / sizeof(Potentiometer))].state, minLEDdim);
    }
    // Print the LED dimLevel to the serial monitor
    //Serial.print(allLEDs[i].name);
    //Serial.print(": ");
    //Serial.println(allLEDs[i].dimLevel);
  }
  //set the dim level for the simon leds, but use the pot on the left
  for (int i = 0; i < sizeof(simonLEDs) / sizeof(SimonLED); i++){
    if (simonLEDs[i].name != "null") {
      // Use the state of the corresponding potentiometer to set the dimming level
      simonLEDs[i].dimLevel = max(allPots[0].state, minLEDdim);
    }
  }
}

void checkButtons() {
  for (int i = 0; i < sizeof(allButtons) / sizeof(Button); i++) {
    int reading = digitalRead(allButtons[i].pin);

    if (reading != allButtons[i].lastState) {
      allButtons[i].lastDebounceTime = millis();
    }

    if ((millis() - allButtons[i].lastDebounceTime) > debounceDelay) {
      if (reading != allButtons[i].state) {
        allButtons[i].state = reading;

        // Print the button state to the serial monitor for debugging
        Serial.print(allButtons[i].name);
        Serial.print(": ");
        Serial.println(allButtons[i].state == HIGH ? "HIGH" : "LOW");
      }
    }

    allButtons[i].lastState = reading;
  }
}

void controlSimonLEDs() {
  for (int i = 0; i < sizeof(simonLEDs) / sizeof(SimonLED); i++) {
    if (allButtons[i].state == LOW && millis() >= simonLEDs[i].turnOffTime) {
      simonLEDs[i].turnOffTime = millis() + 1000;
      analogWrite(simonLEDs[i].dimPin, simonLEDs[i].dimLevel);
    } else if (millis() >= simonLEDs[i].turnOffTime) {
      analogWrite(simonLEDs[i].dimPin, 0);
    }
  }
}


//------------------------------End Functions Section-----------------------------

void setup() {
  // Initialize the serial communication at baud rate of 9600
  Serial.begin(9600);

  // Initialize switch pins as inputs with pullup resistors
  for(int i = 0; i < sizeof(allSwitches)/sizeof(Switch); i++) {
    pinMode(allSwitches[i].pin1, INPUT_PULLUP);
    if (allSwitches[i].pin3 != 0)
      pinMode(allSwitches[i].pin3, INPUT_PULLUP);
  }

  // Initialize LED pins as outputs
  for(int i = 0; i < sizeof(allLEDs)/sizeof(LEDSet); i++) {
    pinMode(allLEDs[i].dimPin, OUTPUT);
    pinMode(allLEDs[i].pin1, OUTPUT);
    if (allLEDs[i].pin2 != 0)
      pinMode(allLEDs[i].pin2, OUTPUT);
    if (allLEDs[i].pin3 != 0)
      pinMode(allLEDs[i].pin3, OUTPUT);
  }

  // Initialize potentiometer pins as inputs
  for(int i = 0; i < sizeof(allPots) / sizeof(Potentiometer); i++) {
    pinMode(allPots[i].pin, INPUT);
  }

  // Initialize button pins as inputs with pullup resistors
  for (int i = 0; i < sizeof(allButtons) / sizeof(Button); i++) {
    pinMode(allButtons[i].pin, INPUT_PULLUP);
  }

  // Initialize SimonLED pins as outputs
  for (int i = 0; i < sizeof(simonLEDs) / sizeof(SimonLED); i++) {
    pinMode(simonLEDs[i].dimPin, OUTPUT);
  }

}

void loop() {
  checkSwitchStates();
  matchLEDStates();
  checkPotentiometers();
  setLEDDimming();
  updateLEDs();
  checkButtons();
  controlSimonLEDs();
  //delay(500);
  // Other code...
}

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
sw1:1
sw1:2
sw1:3
sw2:1
sw2:2
sw2:3
sw3:1
sw3:2
sw3:3
sw4:1
sw4:2
sw4:3
sw5:1
sw5:2
sw5:3
sw6:1
sw6:2
sw6:3
sw7:1
sw7:2
sw7:3
sw8:1
sw8:2
sw8:3
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
sw9:1
sw9:2
sw9:3
sw10:1
sw10:2
sw10:3
led4:A
led4:C
led5:A
led5:C
led6:A
led6:C
led7:A
led7:C
led8:A
led8:C
led9:A
led9:C
led10:A
led10:C
led11:A
led11:C
led12:A
led12:C
led13:A
led13:C
led14:A
led14:C
led15:A
led15:C
led16:A
led16:C
led17:A
led17:C
led18:A
led18:C
sw11:1
sw11:2
sw11:3
sw12:1
sw12:2
sw12:3
sw13:1
sw13:2
sw13:3
sw14:1
sw14:2
sw14:3
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
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
gnd1:GND
gnd2:GND
pot1:GND
pot1:SIG
pot1:VCC
pot2:GND
pot2:SIG
pot2:VCC
pot3:GND
pot3:SIG
pot3:VCC
gnd3:GND
gnd4:GND
gnd5:GND
vcc1:VCC
vcc2:VCC
vcc3:VCC
vcc4:VCC
gnd6:GND
gnd7:GND
gnd8:GND
led19:A
led19:C
led20:A
led20:C
led21:A
led21:C
led22:A
led22:C