#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <avr/wdt.h> // Include the library for resetting
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
#include <SoftwareSerial.h>
SoftwareSerial BT(10,11); // RX TX
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte led=13;
int i = 0;
bool askingShopOrder = false; // Flag to indicate if we are asking about SHOP ORDER
bool askingItemNumber = false; // Flag to indicate if we are asking about ITEM NUMBER
bool askingEmployeeNumber = false; // Flag to indicate if we are asking about the amount of people
bool buttonPressed = false; // Flag to indicate if the button is pressed
bool setupComplete = false; // Flag to indicate if the setup is completed
String lcdContent = ""; // Variable to store LCD content
const int inputPin = A3; // Replace with the pin number you are using (e.g., 13 for onboard LED)
unsigned long totalTime = 0;
unsigned long startTime = 0;
bool isHigh = false;
const unsigned long printInterval = 5000; // Print interval in milliseconds (10 seconds)unsigned long lastPrintTime = 0;
const int photoeyePin = A8; // the number of the photoeye sensor pin (digital input)
const int relayPin = 29; // the number of the relay pin
const int buttonPin1 = 8; // the number of the first push button pin
int count = 0;//for counting
boolean state = true;// for counting
unsigned long lastPrintTime = 0;
// variables for photoeye sensor
unsigned long lastDetectionMillis = 0;
const long interval = 5000; // 10 seconds in milliseconds
const int photoeyeThreshold = HIGH; // Assuming the sensor is normally HIGH and goes LOW when detecting
bool waitForButtonPress = false;
const int chipSelect = 53;
File dataFile;
RTC_DS1307 RTC;
bool lastKeyPressA=true;
bool allowLCDUpdate = false;
bool setupInProgress = false;
unsigned long setupStartTime = 0;
bool allowBButton = false;
bool aButtonPressed = false;
bool employeeNumberEntered = false;
bool isStopped=false;//for print clean press A to pause all counting and stop relay
bool wasAKeyPressed = false; // Flag to track if 'A' key was pressed in the previous iteration
String fileName;
//unsigned long paperWorkTime = 0;
void setup() {
pinMode(inputPin, INPUT);//relay check on or off state for time
pinMode(photoeyePin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.init();
lcd.init();
lcd.backlight();
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0);
lcd.print("PUSH A TO");
lcd.setCursor(0,1);
lcd.print("START SETUP");
BT.begin(9600);
const char number_of_rows = 4;
const char number_of_columns = 4;
char row_pins[number_of_rows] = {9, 8, 7, 6};
char column_pins[number_of_columns] = {5, 4, 3, 2};
char key_array[number_of_rows][number_of_columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
RTC.begin();
if (! RTC.isrunning()) {
//following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
Serial.print("\nInitializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed. Things to check:");
Serial.println("* Is a card inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* Did you change the chipSelect pin to match your shield or module?");
} else {
Serial.println("Wiring is correct and a card is present.");
}
Keypad k = Keypad(makeKeymap(key_array), row_pins, column_pins, number_of_rows, number_of_columns);
unsigned long onTime = 0;//setup timer total time
bool timerRunning = false;//for timer restart
unsigned long goTime = 0; //setup timer start time
char key_pressed;
while (!setupComplete) {
key_pressed = k.getKey();
if (key_pressed == 'A' && !aButtonPressed) {//&& !timerRunning// Check if 'A' key is pressed and was not previously pressed and setup is not completed
allowLCDUpdate = true;
aButtonPressed=true;
goTime = millis();
timerRunning = true;
clearLCD(); // Clear LCD when 'A' key is pressed
lcd.setCursor(0,0);
lcd.print("ENTERING SETUP");
delay(3000);
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("ITEM NUMBER");
askingItemNumber = true; // Set flag to indicate we are asking SHOP ORDER
buttonPressed = true; // Set buttonPressed to true
delay(500); // Delay for stability
}
if (askingItemNumber && key_pressed == '#' ) { // Check if '#' button is pressed for SHOP ORDER after setup completion
dataFile.println("ITEM NUMBER: " + lcdContent); // Print Item number as file name to sd card
dataFile = SD.open(lcdContent + ".txt", FILE_WRITE);//opens the sd card to start saving
saveFileName(lcdContent);
if (!dataFile) {
Serial.println("Error opening data file!");
}
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("SETUP STARTED");
BT.print("Bluetooth");
BT.print(",");
BT.println();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("ITEM NUMBER: " + lcdContent);
clearLCD();
lcd.setCursor(0, 0);
lcd.print("SHOP ORDER"); // Prompt for ITEM NUMBER
askingShopOrder = true;
askingItemNumber = false; // Set flag to indicate we are asking ITEM NUMBER
} else if (askingShopOrder && key_pressed == '#' ) { // Check if '#' button is pressed for ITEM NUMBER after setup completion
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("SHOP ORDER: " + lcdContent); // Print ITEM NUMBER to serial monitor
clearLCD();
lcd.setCursor(0, 0);
lcd.print("Employee Number"); // Prompt for Amount of People
askingShopOrder = false;
askingEmployeeNumber = true; // Set flag to indicate we are asking Amount of People
} else if (askingEmployeeNumber && key_pressed == '#' && !employeeNumberEntered) { // Check if '#' button is pressed for Amount of People after setup completion
employeeNumberEntered = true;
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("Employee Number: " + lcdContent); // Print Amount of People to serial monitor
clearLCD();
lcd.setCursor(2, 1);
lcd.print("SETUP STARTED");
allowLCDUpdate=false;
delay(2000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Press B To");
lcd.setCursor(3,1);
lcd.print("Clock In");
digitalWrite(relayPin, HIGH);
allowBButton=true;
// Optional: print a message to indicate all information has been received
} else if (key_pressed == '*') { // Check if '*' button is pressed for backspace after setup completion
backspace(); // Call backspace function
//} else if (key_pressed) { // If any other key is pressed after setup completion
//updateLCDContent(key_pressed); // Update LCD content with key pressed
//Serial.print("Y");
} else if (key_pressed) { // If any other key is pressed after setup completion
if (allowLCDUpdate) {
updateLCDContent(key_pressed);
}
}
if (key_pressed == 'B' && allowBButton) {//timerRunning
unsigned long stopTime = millis(); // Record the stop time
unsigned long elapsedTime = stopTime - goTime;
//prints for setup time and time in seconds
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("SETUP TIME: ");
dataFile.println(elapsedTime/ 1000);
Serial.print(elapsedTime/ 1000);
//prints for job start
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("JOB STARTED");
timerRunning = false;
goTime = 0;
stopTime=0;
lastPrintTime = millis();
clearLCD(); // Clear the LCD
timerRunning=false;
lcd.setCursor(0,0);
lcd.print("CLOCKED IN");
lcd.setCursor(0,1);
lcd.print("ON");
delay(2000);
digitalWrite(relayPin, HIGH);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PART: ");
count = 0;
lcd.setCursor(0,1);
lcd.print("ON");
dataFile.flush();
lastDetectionMillis = millis(); // Reset the timer
setupComplete = true;
dataFile.println("");
lastDetectionMillis = millis(); // Reset the timer
}
}
}
void saveFileName(String name) {
fileName = name;
}
void updateLCDContent(char key) {
if (i < 16) {
lcd.setCursor(i, 1);
lcd.print(key);
lcdContent += key; // Add the pressed key to lcdContent
i++;
}
}
void clearLCD() {
lcd.clear();
lcd.setCursor(0, 1); // Set cursor to the beginning of the second row
i = 0;
lcdContent = ""; // Clear lcdContent variable
}
void backspace() {
if (i > 0) {
i--; // Move cursor back
lcd.setCursor(i, 1);
lcd.print(" "); // Clear character on LCD
lcd.setCursor(i, 1);
lcdContent.remove(i, 1); // Remove last character from lcdContent
}
}
void loop(){
const char number_of_rows = 4;
const char number_of_columns = 4;
char row_pins[number_of_rows] = {9, 8, 7, 6};
char column_pins[number_of_columns] = {5, 4, 3, 2};
char key_array[number_of_rows][number_of_columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad k = Keypad(makeKeymap(key_array), row_pins, column_pins, number_of_rows, number_of_columns);
char key_pressed = k.getKey();
int currentState = digitalRead(inputPin);
if (currentState == HIGH && !isHigh) {
// Pin just went HIGH
startTime = millis();
isHigh = true;
digitalWrite(led,LOW);//for red light when relay is ON
} else if (currentState == LOW && isHigh) {
// Pin just went LOW
unsigned long duration = millis() - startTime;
totalTime += duration;
isHigh = false;
digitalWrite(led,HIGH);// for red light when relay is OFF
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("MACHINE TIMED OUT (OFF)");
}
if (key_pressed == 'A' && !wasAKeyPressed) {
if (!isStopped) {
// Stop the sensor and trigger relay immediately
digitalWrite(relayPin, LOW); // Assuming LOW triggers the relay
waitForButtonPress = true; // Set the flag to wait for button press
//lastDetectionMillis = millis(); // Reset the timer
isStopped = true; // Set the flag to indicate that relay and counting are stopped
lcd.setCursor(0,1);
lcd.print("Print Cleaning");
Serial.println("Cleaning");
} else {
// Resume normal operation
digitalWrite(relayPin, HIGH); // Assuming HIGH stops the relay
isStopped = false; // Set the flag to indicate that relay and counting are resumed
lcd.setCursor(0,1);
lcd.print("ON ");
Serial.println("Resume function");
lastDetectionMillis = millis();
waitForButtonPress = false;
}
}
// Update the flag for 'A' key press in the current iteration
wasAKeyPressed = (key_pressed == 'A');
// Check if the button is pressed and print the total time
if (key_pressed == '#') {
delay(250);
digitalWrite(relayPin, LOW);//turn relay off
digitalWrite(led,HIGH);// for red light when relay is OFF
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Final Count?");
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Total Time Running: ");
dataFile.println(totalTime / 1000); // Convert milliseconds to seconds
Serial.print("Total Time Running: ");
Serial.print(totalTime / 1000);
// Add total elapsed time
unsigned long elapsedMillis = millis() - lastPrintTime;
//writes to sd card
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Total Elapsed Time: ");
dataFile.println(elapsedMillis / 1000); // Convert milliseconds to seconds
Serial.print("Total Elapsed Time: ");
Serial.print(elapsedMillis / 1000);
//writes to sd card
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Total part count: "); //DIsplay total count in serial monitor
dataFile.println(count);
Serial.print("Total part count: ");//display total count in serial monitor
Serial.print(count);
if (count > 0) { // parts per hour testing
float partsPerHour = float(count) / (elapsedMillis / 60000.0); // Calculate parts per hour Total ELpased TIme
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Parts Per Minute Elapsed Time: ");
Serial.print("Parts Per Minute Elapsed Time: ");
dataFile.println(partsPerHour); //parts per hour testing
Serial.print(partsPerHour);
}
if (count > 0) { // parts per hour testing
float partsPerHour = float(count) / (totalTime / 60000); // Calculate parts per hour Total ELpased TIme
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Parts Per Minute Running Time: ");
Serial.print("Parts Per Minute Running Time: ");
dataFile.println(partsPerHour); //parts per hour testing
Serial.print(partsPerHour);
delay(3000);
//wdt_enable(WDTO_15MS); // Enable watchdog timer (resets the Arduino)
// while (1);
}
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("END JOB");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Final Count?");
//Paperwork timer Started at fianl count
unsigned long cleanTime=0;
unsigned long doneTime=0;
if(cleanTime ==0){
cleanTime=millis();
}
//when the # sign is pushed again it goes to the final count screen
int lcdIndex = 0;
char lcdContent[16]; // Assuming lcdContent is an array of chars
while (true) {
char key = k.getKey();
if (key != NO_KEY) {
if (key == '#') {
//start a timer
// Print the typed number
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Good Pieces: ");
dataFile.println(lcdContent);
lcdContent[0] = '\0';
lcdIndex = 0;
break; // Exit the loop
} else if (key == '*') { // Check if '*' button is pressed for backspace after setup completion
if (lcdIndex > 0) {
lcdIndex--; // Move the index back
lcdContent[lcdIndex] = '\0'; // Null terminate the string at the new index
lcd.setCursor(lcdIndex, 1);
lcd.print(" "); // Print whitespace to clear the character
}
} else {
// Append the pressed key to lcdContent if within bounds
if (lcdIndex < 15) {
lcdContent[lcdIndex++] = key;
lcdContent[lcdIndex] = '\0'; // Null terminate the string
// Update the LCD display with the current content
lcd.setCursor(0, 1);
lcd.print(lcdContent);
}
}
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Bad Piece Count");
//FOr Bad piece quantities
while (true) {
char key = k.getKey();
if (key != NO_KEY) {
if (key == '#') {
//start a timer
// Print the typed number
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Bad Piece Count: ");
dataFile.println(lcdContent);
lcdContent[0] = '\0';
lcdIndex = 0;
break; // Exit the loop
} else if (key == '*') { // Check if '*' button is pressed for backspace after setup completion
if (lcdIndex > 0) {
lcdIndex--; // Move the index back
lcdContent[lcdIndex] = '\0'; // Null terminate the string at the new index
lcd.setCursor(lcdIndex, 1);
lcd.print(" "); // Print whitespace to clear the character
}
} else {
// Append the pressed key to lcdContent if within bounds
if (lcdIndex < 15) {
lcdContent[lcdIndex++] = key;
lcdContent[lcdIndex] = '\0'; // Null terminate the string
// Update the LCD display with the current content
lcd.setCursor(0, 1);
lcd.print(lcdContent);
}
}
}
}
// Reset Arduino after data file flush
dataFile.flush();
lcd.clear();
lcd.print("Resetting");
if (cleanTime != 0) {
doneTime = millis() - cleanTime;
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.print("Paperwork Elapsed Time: ");
dataFile.println(doneTime/1000);
dataFile.flush();
// read from the file until there's nothing else in it:
File dataFile = SD.open(fileName + ".txt", FILE_READ);
if (dataFile) {
while (dataFile.available()) {
String line = dataFile.readStringUntil('\n');
// Parse the line
int colonIndex = line.indexOf(':');
if (colonIndex != -1) {
String timestamp = line.substring(0, colonIndex);
String eventData = line.substring(colonIndex - 2);
// Process the parsed data (e.g., print to Serial monitor)
Serial.println(eventData);
BT.println(eventData);
delay(500);
}
}
dataFile.close();
} else {
Serial.println("Error opening file");
}
Serial.print("DATA FILE CLOSED");
delay(15000);
lcd.clear();
}
}
// Read the state of the photoeye sensor (digital):
int sensorValue = digitalRead(photoeyePin);
// Check if waiting for button press
if(waitForButtonPress){
if (key_pressed == '1') {
waitForButtonPress = false; // Button has been pressed, reset the flag
lastDetectionMillis = millis(); // Reset the timer
digitalWrite(relayPin, HIGH);
delay(50);
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("OTHER(MACHINE TURNED ON)");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("ON");
} else if (key_pressed == '2') {
waitForButtonPress = false; // Button has been pressed, reset the flag
lastDetectionMillis = millis(); // Reset the timer
digitalWrite(relayPin, HIGH);
delay(50);
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("MACHINE ISSUE(MACHINE TURNED ON)");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("ON");
} else if (key_pressed == '3') {
waitForButtonPress = false; // Button has been pressed, reset the flag
lastDetectionMillis = millis(); // Reset the timer
digitalWrite(relayPin, HIGH);
delay(50);
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("PRINT ISSUE(MACHINE TURNED ON)");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("ON");
} else if (key_pressed == '4') {
waitForButtonPress = false;
lastDetectionMillis = millis(); // Reset the timer /// delete if no good to the bottom line.
digitalWrite(relayPin, HIGH);
delay(50);
//writes to sd card
DateTime now = RTC.now();
if(now.hour()<10)
dataFile.print('0');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
if(now.minute()<10)
dataFile.print('0');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
if(now.second()<10)
dataFile.print('0');
dataFile.print(now.second(), DEC);
dataFile.print(" ");
dataFile.println("BREAK TIME"); // delete if no good
lcd.setCursor(0, 1);
lcd.print("Break Time ");
}
} else {
// Check if the sensor has been continuously in the HIGH state for more than 10 seconds
if (sensorValue == photoeyeThreshold) {
// If 1 state hasn't been detected in the last 10 seconds, trigger the relay
unsigned long currentMillis = millis();
if (currentMillis - lastDetectionMillis >= interval) {
digitalWrite(relayPin, LOW);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(1, 1);
lcd.print("WHAT HAPPENED?");
waitForButtonPress = true; // Set the flag to wait for button press
// Reset the timer
lastDetectionMillis = currentMillis;
}
} else {
// Reset the timer if 1 state is detected
lastDetectionMillis = millis();
}
if (!digitalRead(photoeyePin) && state) {
count++;
state = false;
Serial.print("its counting");
lcd.setCursor(5, 0);
lcd.print(count);
delay(500);
}
if (digitalRead(photoeyePin)) {
state = true;
delay(400);
}
}
}