#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Define the pins for switches
#define SWITCH_S1_PIN 2  // Assign pin 2 to switch S1
#define SWITCH_S2_PIN 3  // Assign pin 3 to switch S2
#define SWITCH_S3_PIN 4  // Assign pin 4 to switch S3

// Define the pins for the shift register
#define DS_PIN 11        // Data pin for shift register
#define SH_CP_PIN 12     // Shift register clock pin
#define ST_CP_PIN 10     // Storage register clock pin

// Define the number of relays
#define NUM_RELAYS 8     // Number of relays connected

// Define relay states
bool relayStates[NUM_RELAYS] = {false};  // Initialize relay states as false (off)

// Flag to indicate whether the system is ready for processing
bool systemReady = false;

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // Initialize LCD
  lcd.init();
  // Turn on the backlight
  lcd.backlight();

  // Set the switch pins as inputs with pull-up resistors
  pinMode(SWITCH_S1_PIN, INPUT_PULLUP);
  pinMode(SWITCH_S2_PIN, INPUT_PULLUP);
  pinMode(SWITCH_S3_PIN, INPUT_PULLUP);

  // Set shift register pins as outputs
  pinMode(DS_PIN, OUTPUT);
  pinMode(SH_CP_PIN, OUTPUT);
  pinMode(ST_CP_PIN, OUTPUT);

  // Display "HELLO :)" for 5 seconds
  lcd.setCursor(0, 0);
  lcd.print("    HELLO :)");
  delay(5000);
  lcd.clear();

  // Print initial message
  lcd.setCursor(0, 0);
  lcd.print("ENSURE THAT THE");
  lcd.setCursor(0, 1);
  lcd.print(" DOOR IS CLOSED");
  delay(5000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("PLEASE SELECT A");
  lcd.setCursor(0, 1);
  lcd.print("PROCESS TO START");
}

void loop() {
  // Check if switch S1 is pressed
  if (digitalRead(SWITCH_S1_PIN) == LOW) {
    // Display "Initialising..." message
    lcd.setCursor(0, 0);
    lcd.print("INITIALISING...                                                           ");
    delay(1);
    // Turn on relay R1
    setRelayState(0, true);

    // Wait for switch S2 to be pressed
    while (digitalRead(SWITCH_S2_PIN) == HIGH) {
      delay(100); // Adjust delay as necessary
    }

    // Clear LCD after S2 is pressed
    lcd.clear();

    // Set the system ready flag
    systemReady = true;
  }

  // Check if the system is ready for processing
  if (systemReady) {
    // Check if switch S2 is pressed to turn off relay R1
    if (digitalRead(SWITCH_S2_PIN) == LOW) {
      setRelayState(0, false); // Turn off relay R1
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("  DOOR LOCKED");
      delay(3000);
      lcd.clear();
    }
    if (digitalRead(SWITCH_S2_PIN) == HIGH) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("   WASHING...");
      delay(3);
      setRelayState(2, true);
      setRelayState(3, true);
      delay(5000000);  //15 min
    }
  }

  // Check if switch S3 is pressed to stop everything
  if (digitalRead(SWITCH_S3_PIN) == LOW && systemReady) {
    // Display "Stopping process" on LCD for 5 seconds
    setRelayState(0, false);
    setRelayState(1, false);
    setRelayState(2, false);
    setRelayState(3, false);
    setRelayState(4, false);
    setRelayState(5, false);
    setRelayState(6, false);
    setRelayState(7, false);

    delay(100);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Stopping process");
    delay(500); // Wait for 5 seconds

    // Turn on relay 2 for 5 seconds
    setRelayState(1, true);
    delay(5000); // Wait for 5 seconds
    setRelayState(1, false); // Turn off relay 2

    // Clear LCD after stopping process
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PLEASE SELECT A");
    lcd.setCursor(0, 1);
    lcd.print("PROCESS TO START");

    // Reset system ready flag
    systemReady = false;
  }
}

// Function to set relay state
void setRelayState(int relayIndex, bool state) {
  if (relayIndex >= 0 && relayIndex < NUM_RELAYS) {
    relayStates[relayIndex] = state;
    // Shift out data to the shift register
    digitalWrite(ST_CP_PIN, LOW);
    for (int i = NUM_RELAYS - 1; i >= 0; --i) {
      digitalWrite(DS_PIN, relayStates[i]);
      digitalWrite(SH_CP_PIN, HIGH);
      digitalWrite(SH_CP_PIN, LOW);
    }
    digitalWrite(ST_CP_PIN, HIGH);
  }
}
$abcdeabcde151015202530354045505560fghijfghij
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
74HC595