#include <avr/wdt.h>

#define D2 2
#define D3 3
#define D4 4
#define D5 5
#define D6 6
#define A0_PIN A0
#define D10 10
#define D11 11
#define D12 12

int potValue = 0;
int timerDuration = 0;
unsigned long previousMillis = 0;
const long debounceDelay = 50;

void setup() {
  pinMode(D2, INPUT_PULLUP);
  pinMode(D3, INPUT_PULLUP);
  pinMode(D4, INPUT_PULLUP);
  pinMode(D5, INPUT_PULLUP);
  pinMode(D6, INPUT_PULLUP);
  pinMode(A0_PIN, INPUT);
  pinMode(D10, OUTPUT);
  pinMode(D11, OUTPUT);
  pinMode(D12, OUTPUT);
  
  digitalWrite(D10, LOW);
  digitalWrite(D11, LOW);
  digitalWrite(D12, LOW);
  
  wdt_enable(WDTO_8S); // Enable watchdog with 8-second timeout
}

void loop() {
  wdt_reset(); // Reset the watchdog timer

  int d2State = digitalRead(D2);
  int d3State = digitalRead(D3);
  int d4State = digitalRead(D4);
  int d5State = digitalRead(D5);
  int d6State = digitalRead(D6);

  // Washer
  if (d2State == LOW && d6State == LOW) {
    digitalWrite(D12, HIGH);
    while (digitalRead(D6) == LOW) {
      digitalWrite(D10, HIGH);
      delay(15000); // 15 seconds
      digitalWrite(D10, LOW);
    }
    digitalWrite(D12, LOW);
  }
  // Off
  else if (d2State == LOW) {
    digitalWrite(D10, LOW);
    digitalWrite(D11, LOW);
  }
  // Pulse
  else if (d3State == LOW) {
    digitalWrite(D10, HIGH);
    delay(500); // 0.5 seconds
    digitalWrite(D10, LOW);
    timerDuration = map(analogRead(A0_PIN), 0, 1023, 5000, 50000); // Mapping analog input to timer duration (5-50 seconds)
    delay(timerDuration);
  }
  // Low
  else if (d4State == LOW) {
    digitalWrite(D10, HIGH);
  }
  // High
  else if (d5State == LOW) {
    digitalWrite(D10, HIGH);
    digitalWrite(D11, HIGH);
  }
  else {
    digitalWrite(D12, LOW); // Ensure D12 is low if none of the conditions are met
  }
  
  delay(10); // Small delay to debounce
}
