#include <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
// -------- RFID --------
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
// -------- LCD --------
LiquidCrystal_I2C lcd(0x27, 16, 2);
// -------- Button --------
int buttonPin = 2;
bool machineRunning = false;
// -------- Timing --------
unsigned long startTime = 0;
unsigned long duration = 0;
unsigned long lastPressTime = 0;
// -------- Worker --------
String workerName = "None";
// -------- SETTINGS --------
const int OFF_DELAY = 1000; // 1 sec delay for stability
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT_PULLUP);
lcd.print("System Ready");
delay(2000);
lcd.clear();
}
void loop() {
// -------- BUTTON LOGIC --------
int buttonState = digitalRead(buttonPin);
// Pressed (LOW)
if (buttonState == LOW) {
lastPressTime = millis();
if (!machineRunning) {
machineRunning = true;
startTime = millis();
}
}
// Released → turn OFF after delay
if (machineRunning && (millis() - lastPressTime > OFF_DELAY)) {
machineRunning = false;
duration = (millis() - startTime) / 1000;
}
// -------- RFID --------
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
uid += String(rfid.uid.uidByte[i], HEX);
}
uid.toLowerCase();
if (uid == "a35f2c91") {
workerName = "Ravi";
}
else if (uid == "b4129af3") {
workerName = "Vinay";
}
else {
workerName = "Ashwin";
}
rfid.PICC_HaltA();
}
// -------- DISPLAY --------
lcd.clear();
unsigned long currentDuration;
if (machineRunning) {
currentDuration = (millis() - startTime) / 1000;
} else {
currentDuration = duration;
}
int minutes = currentDuration ;
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(minutes);
lcd.print(" min");
lcd.setCursor(0, 1);
lcd.print("Worker: ");
lcd.print(workerName);
delay(500);
}Loading
mfrc522
mfrc522