#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 7; // Change this to the pin you're using for the switch
const int buzzerPin = 8; // Change this to the pin you're using for the buzzer
const int initialHours = 6;
const int initialMinutes = 0;
const int initialSeconds = 0; // Start from 10 seconds for testing
unsigned long countdownEndTime = 0;
bool countdownStarted = false;
bool switchState = false;
bool timeLapseRunning = false;
unsigned long timeLapseStartTime = 0;
void setup() {
lcd.begin(20, 4);
pinMode(switchPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
lcd.print("Switch OFF to start");
}
void loop() {
switchState = digitalRead(switchPin);
if (switchState && !countdownStarted) {
lcd.clear();
lcd.print("LN2 Tracking System:");
countdownEndTime = millis() + initialHours * 3600000 + initialMinutes * 60000 + initialSeconds * 1000;
countdownStarted = true;
} else if (!switchState) {
countdownStarted = false;
timeLapseRunning = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("___LN2 Monitoring___");
lcd.setCursor(2, 1);
lcd.print("LN2 not Detected!");
lcd.setCursor(2, 2);
lcd.print("Open the valve to");
lcd.setCursor(2, 3);
lcd.print("start monitoring");
}
if (countdownStarted) {
unsigned long currentTime = millis();
unsigned long remainingTime = countdownEndTime > currentTime ? countdownEndTime - currentTime : 0;
int hours = remainingTime / 3600000;
int minutes = (remainingTime % 3600000) / 60000;
int seconds = (remainingTime % 60000) / 1000;
lcd.setCursor(1, 1);
lcd.print("Time Left: ");
lcd.print(hours);
lcd.print(":");
if (minutes < 10) lcd.print("0");
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) lcd.print("0");
lcd.print(seconds);
if (remainingTime == 0 && !timeLapseRunning) {
lcd.setCursor(1, 2);
lcd.print("LN2 must be Empty!!");
timeLapseStartTime = millis();
timeLapseRunning = true;
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
}
}
if (timeLapseRunning) {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - timeLapseStartTime;
int lapseHours = elapsedTime / 3600000;
int lapseMinutes = (elapsedTime % 3600000) / 60000;
int lapseSeconds = (elapsedTime % 60000) / 1000;
lcd.setCursor(0, 3);
lcd.print("Time Lapsed: ");
lcd.print(lapseHours);
lcd.print(":");
if (lapseMinutes < 10) lcd.print("0");
lcd.print(lapseMinutes);
lcd.print(":");
if (lapseSeconds < 10) lcd.print("0");
lcd.print(lapseSeconds);
}
if (!timeLapseRunning) {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
delay(1000); // Update every second
}