#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <RTClib.h>
// Initialize components
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change the address if needed
RTC_DS1307 rtc;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {1, 2, 3, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 6, 7, 0}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int buttonPin1 = 9;
const int buttonPin2 = 10;
const int restartButtonPin = 8;
const int ledR = 13;
const int ledB = 11;
const int ledG = 12;
bool counting = false;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
enum EntryState {
SHIFT,
MACHINE,
STOP_CODE,
COUNTING
};
EntryState currentEntryState = SHIFT;
String enteredCode = ""; // Store entered code
bool restartPending = false; // Variable to track restart button press
void setup() {
lcd.init();
lcd.backlight(); // Turn on the backlight
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set RTC to compile time
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(restartButtonPin, INPUT_PULLUP); // Set the restart button pin as input with internal pull-up resistor
lcd.setCursor(0, 0);
lcd.print("Enter the shift:");
}
void loop() {
// Check for restart button press
if (digitalRead(restartButtonPin) == LOW) {
restartPending = true;
}
if (restartPending) {
// Clear any previous data and reset variables
enteredCode = "";
currentEntryState = SHIFT;
counting = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter the shift:");
restartPending = false; // Reset the restart flag
}
char key = keypad.getKey();
if (key && !counting) {
if (currentEntryState == SHIFT || currentEntryState == MACHINE || currentEntryState == STOP_CODE) {
if (key == '#') {
if (enteredCode.length() > 0) {
// Transition to the next entry state
switch (currentEntryState) {
case SHIFT:
currentEntryState = MACHINE;
break;
case MACHINE:
currentEntryState = STOP_CODE;
break;
case STOP_CODE:
currentEntryState = COUNTING;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press # to start");
break;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter ");
switch (currentEntryState) {
case MACHINE:
lcd.print("Machine :");
break;
case STOP_CODE:
lcd.print("Stop code :");
break;
}
enteredCode = ""; // Clear entered code for the next entry
}
}
else if (key == '*') { // Check if "*" key is pressed
enteredCode = ""; // Clear entered code
lcd.setCursor(6, 1);
lcd.print(" "); // Clear the display for entered code
}
else {
enteredCode += key;
lcd.setCursor(6, 1);
lcd.print(enteredCode);
}
}
}
if (currentEntryState == COUNTING) {
if (digitalRead(buttonPin1) == LOW) {
if (!counting) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Counting...");
counting = true;
digitalWrite(ledG, LOW);
digitalWrite(ledR, HIGH);
startTime = millis();
elapsedTime = 0; // Reset elapsed time
}
}
if (counting) {
elapsedTime = millis() - startTime;
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(elapsedTime / 60000); // Display time in minutes
lcd.print(" min ");
lcd.print((elapsedTime / 1000) % 60); // Display time in seconds
lcd.print(" sec");
}
if (digitalRead(buttonPin2) == LOW) {
if (counting) {
counting = false;
digitalWrite(ledR, LOW);
digitalWrite(ledG, HIGH);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)).unixtime() + (elapsedTime / 1000)); // Set RTC to current time + elapsed time
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count stopped");
lcd.setCursor(0, 1);
lcd.print("Downtime: ");
lcd.setCursor(3, 2);
lcd.print(elapsedTime / 60000); // Display time in minutes
lcd.print(" min ");
lcd.print((elapsedTime / 1000) % 60); // Display time in seconds
lcd.print(" sec ");
DateTime now = rtc.now();
lcd.setCursor(0, 3);
lcd.print("Date: ");
lcd.print(now.year(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.day(), DEC);
}
}
}
}