// Pin Definitions
const int Sw0PotPin = A0;
const int Sw1OffParkPin = 2;
const int Sw1IntmPin = 3;
const int Sw1LoPin = 4;
const int Sw1HiPin = 5;
const int Rly1LoPin = 6;
const int Rly2HiPin = 7;
const int Rly3GndPin = 8;
const int Rly4WshPin = 9;

// Global Variables
int waitingTime = 5000; // Default waiting time in milliseconds (5 seconds)
bool isWipIntmRunning = false; // Flag to track if WipIntm function is running
unsigned long previousTime = 0; // Variable to store the previous time for checking elapsed time

void setup() {
  // Initialize input pins
  pinMode(Sw0PotPin, INPUT);
  pinMode(Sw1OffParkPin, INPUT);
  pinMode(Sw1IntmPin, INPUT);
  pinMode(Sw1LoPin, INPUT);
  pinMode(Sw1HiPin, INPUT);

  // Initialize output pins
  pinMode(Rly1LoPin, OUTPUT);
  pinMode(Rly2HiPin, OUTPUT);
  pinMode(Rly3GndPin, OUTPUT);
  pinMode(Rly4WshPin, OUTPUT);

  // Default state: all relays off
  digitalWrite(Rly1LoPin, HIGH); //was low
  digitalWrite(Rly2HiPin, HIGH); //was low
  digitalWrite(Rly3GndPin, HIGH); //was low
  digitalWrite(Rly4WshPin, HIGH); //was low
}

void loop() {
  // Read input states
  int offParkState = digitalRead(Sw1OffParkPin);
  int intmState = digitalRead(Sw1IntmPin);
  int loState = digitalRead(Sw1LoPin);
  int hiState = digitalRead(Sw1HiPin);

  // Check input states and execute corresponding functions
  if (offParkState == HIGH) {
    WipOffPark();
  } else if (intmState == HIGH) {
    if (!isWipIntmRunning) {
      WipIntm();
    }
  } else if (loState == HIGH) {
    WipLo();
  } else if (hiState == HIGH) {
    WipHi();
  } else {
    // Stop WipIntm function if D3 is set low/off
    if (isWipIntmRunning) {
      isWipIntmRunning = false;
      digitalWrite(Rly3GndPin, LOW);
    }
  }

  // Update waiting time based on potentiometer value
  int potValue = analogRead(Sw0PotPin);
  waitingTime = map(potValue, 0, 1023, 5000, 30000); // 5 seconds to 30 seconds

  // Check if the waiting time has elapsed for WipIntm function
  if (isWipIntmRunning && millis() - previousTime >= waitingTime) {
    previousTime = millis(); // Update the previous time for the next waiting period
    digitalWrite(Rly3GndPin, LOW);
    delay(1000);
    digitalWrite(Rly3GndPin, HIGH);
  }
}

void WipOffPark() {
  digitalWrite(Rly1LoPin, LOW);
  digitalWrite(Rly2HiPin, LOW);
  digitalWrite(Rly3GndPin, HIGH);
  digitalWrite(Rly4WshPin, HIGH);
}

void WipIntm() {
  isWipIntmRunning = true; // Set the flag to indicate that WipIntm function is running
  digitalWrite(Rly1LoPin, LOW);
  digitalWrite(Rly2HiPin, LOW);
  digitalWrite(Rly3GndPin, HIGH);
  digitalWrite(Rly4WshPin, HIGH);

  while (digitalRead(Sw1IntmPin) == HIGH && isWipIntmRunning) {
    // WipIntm function code
    int potValue = analogRead(Sw0PotPin);
    waitingTime = map(potValue, 0, 1023, 5000, 30000); // Update waiting time based on potentiometer value

    unsigned long currentTime = millis();
    if (currentTime - previousTime >= waitingTime) {
      previousTime = currentTime;
      digitalWrite(Rly3GndPin, LOW);
      delay(1000);
      digitalWrite(Rly3GndPin, HIGH);
    }
  }

  isWipIntmRunning = false; // Reset the flag after the loop ends
  digitalWrite(Rly3GndPin, HIGH);
}

void WipLo() {
  digitalWrite(Rly1LoPin, LOW);
  digitalWrite(Rly2HiPin, LOW);
  digitalWrite(Rly3GndPin, LOW);
  digitalWrite(Rly4WshPin, HIGH);
}

void WipHi() {
  digitalWrite(Rly1LoPin, HIGH);
  digitalWrite(Rly2HiPin, LOW);
  digitalWrite(Rly3GndPin, HIGH);
  digitalWrite(Rly4WshPin, LOW);
}
