//============================start==============================

# define buzzer_pin 2
# define bottlecapacity_pin 34
# define fullvatswitch_pin 13
# define ps_pin 22
# define resinemptyled_pin 23

unsigned long previousMillis1 = 0UL;  //resin bottle low warning LED timer on
unsigned long previousMillis2 = 0UL;  //resin bottle low warning LED timer off
unsigned long previousMillis3 = 0UL;  //resin bottle low warning buzzer timer on
unsigned long previousMillis4 = 0UL;  //resin bottle low warning buzzer timer off
unsigned long previousMillis5 = 0UL;  //autofill pump off delay timer
unsigned long previousMillis6 = 0UL;  //autofill pump on delay timer
unsigned long previousMillis7 = 0UL;  //start delay on power up

unsigned long interval1 = 3000UL;   //resin bottle low warning LED timer on
unsigned long interval2 = 5000UL;   //resin bottle low warning LED timer off
unsigned long interval3 = 1000UL;   //resin bottle low warning buzzer timer on
unsigned long interval4 = 10000UL;  //resin bottle low warning buzzer timer off
unsigned long interval5 = 10000UL;  //autofill pump off delay timer
unsigned long interval6 = 60000UL;  //autofill pump on delay timer
unsigned long interval7 = 5000UL;   //start delay on power up

unsigned long currentMillis = 0;
byte bottlecapacity = 0; // Initialize to 0
byte vatFull = 0; // Initialize to 0

void setup() {
  // Define pins
  pinMode(bottlecapacity_pin, INPUT);  // Bottle capacity switch pin
  pinMode(fullvatswitch_pin, INPUT);   // Full vat switch pin
  pinMode(ps_pin, OUTPUT);   // Solenoids and pump on
  pinMode(buzzer_pin, OUTPUT);  // Resin bottle empty buzzer
  pinMode(resinemptyled_pin, OUTPUT);  // Resin bottle empty LED
}

void loop() {
  currentMillis = millis();

  if (currentMillis > interval7) // 30sec delay before code starts operating
  {
    resinbottlecapacity();
    vatcapacity();
    emptyresinbottlewarning();
  }
}

//=====================================================Sketch function 1 - check resin bottle level:========================================================

// If bottle is empty, do not start pump and recheck bottle level
void resinbottlecapacity()  {
  bottlecapacity = digitalRead(bottlecapacity_pin); // Read bottle capacity pin

  if (!bottlecapacity) {
    if (currentMillis - previousMillis5 >= interval5) {
      digitalWrite(ps_pin, LOW);  // Stop resin pump because of an empty resin bottle
    }
  }
}

// Low resin bottle warning indicator LED and buzzer
void emptyresinbottlewarning() {
  bottlecapacity = digitalRead(bottlecapacity_pin); // Read bottle capacity pin

  // If resin bottle is empty, turn on empty resin bottle warning LED and buzzer
  if (!bottlecapacity) {
    if (currentMillis - previousMillis1 >= interval1) { // If resin bottle is low, turn on low resin bottle indicator LED and buzzer after interval 1
      digitalWrite(resinemptyled_pin, HIGH);
      previousMillis1 = currentMillis;
    }

    if (currentMillis - previousMillis2 >= interval2) { // If resin bottle is low, turn off low resin bottle indicator LED and buzzer after interval 2
      digitalWrite(resinemptyled_pin, LOW);
      previousMillis2 = currentMillis;
    }

    if (currentMillis - previousMillis3 >= interval3) { // If resin bottle is low, turn on low resin bottle indicator LED and buzzer after interval 3
      digitalWrite(buzzer_pin, HIGH);
      previousMillis3 = currentMillis;
    }

    if (currentMillis - previousMillis4 >= interval4) {  // If resin bottle is low, turn on low resin bottle indicator LED and buzzer after interval 4
      digitalWrite(buzzer_pin, LOW);
      previousMillis4 = currentMillis;
    }
  }
}

//===============================================Sketch function 2 - autofill pump control===================================================================

void vatcapacity() {
  vatFull = digitalRead(fullvatswitch_pin); // Read vat capacity pin

  if (currentMillis - previousMillis5 >= interval5) { // If time interval5 has been reached
    if (vatFull || !bottlecapacity) { // If resin bottle is low and vat is full
      digitalWrite(ps_pin, LOW); // Turn off autofill after interval5   
    }
    previousMillis5 = currentMillis;
  } else if (currentMillis - previousMillis6 >= interval6) { // If time interval6 has been reached
    if (!vatFull && bottlecapacity) { // If resin bottle is full and vat is low
      digitalWrite(ps_pin, HIGH); // Turn on autofill after interval6
    }
    previousMillis6 = currentMillis;
  }
}

//============================================================END======================================================================================
NOCOMNCVCCGNDINLED1PWRRelay Module