#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <avr/wdt.h> // Include the library for resetting
LiquidCrystal_I2C lcd(0x27, 16, 2);
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 = 7; // the number of the photoeye sensor pin (digital input)
const int relayPin = 13; // 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;
void setup() {
pinMode(inputPin, INPUT);//relay check on or off state for time
pinMode(photoeyePin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.init();
lcd.init();
lcd.backlight();
lcd.setBacklight(HIGH);
lcd.setCursor(5, 0);
lcd.print("PUSH A");
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);
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' && !timerRunning) {// Check if 'A' key is pressed and was not previously pressed and setup is not completed
goTime = millis();
timerRunning = true;
clearLCD(); // Clear LCD when 'A' key is pressed
lcd.setCursor(0,0);
lcd.print("ENTERING SETUP");
delay(2000);
Serial.println("SETUP STARTED");
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("SHOP ORDER");
askingShopOrder = true; // Set flag to indicate we are asking SHOP ORDER
buttonPressed = true; // Set buttonPressed to true
delay(1000); // Delay for stability
//}
//if (key_pressed != 'A') { // Check if 'A' key is released
// buttonPressed = false; // Set buttonPressed to false
}
if (askingShopOrder && key_pressed == '#' ) { // Check if '#' button is pressed for SHOP ORDER after setup completion
Serial.println("SHOP ORDER: " + lcdContent); // Print SHOP ORDER to serial monitor
clearLCD();
lcd.setCursor(0, 0);
lcd.print("ENTER ITEM"); // Prompt for ITEM NUMBER
askingShopOrder = false;
askingItemNumber = true; // Set flag to indicate we are asking ITEM NUMBER
} else if (askingItemNumber && key_pressed == '#' ) { // Check if '#' button is pressed for ITEM NUMBER after setup completion
Serial.println("ITEM NUMBER: " + lcdContent); // Print ITEM NUMBER to serial monitor
clearLCD();
lcd.setCursor(0, 0);
lcd.print("Employee Number"); // Prompt for Amount of People
askingItemNumber = false;
askingEmployeeNumber = true; // Set flag to indicate we are asking Amount of People
} else if (askingEmployeeNumber && key_pressed == '#' ) { // Check if '#' button is pressed for Amount of People after setup completion
Serial.println("Employee Number:" + lcdContent); // Print Amount of People to serial monitor
clearLCD();
lcd.setCursor(2, 1);
lcd.print("SETUP STARTED");
delay(10000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Press B To");
lcd.setCursor(3,1);
lcd.print("Clock In");
digitalWrite(relayPin, HIGH);
// 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
}
if (key_pressed == 'B' && timerRunning) {
unsigned long stopTime = millis(); // Record the stop time
unsigned long elapsedTime = stopTime - goTime;
Serial.print("Total Time Running: ");
Serial.println(elapsedTime/ 1000);
timerRunning = false;
goTime = 0;
stopTime=0;
lastPrintTime = millis();
digitalWrite(relayPin, HIGH);
delay(5000);
clearLCD(); // Clear the LCD
Serial.print("JOB START");
lcd.setCursor(0,0);
lcd.print("CLOCKED IN");
delay(10000);
setupComplete = true;
Serial.println("");
lastDetectionMillis = millis(); // Reset the timer
}
}
}
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 = 3;
const char number_of_columns = 3;
char row_pins[number_of_rows] = {9, 8, 7};
char column_pins[number_of_columns] = {5, 4, 3};
char key_array[number_of_rows][number_of_columns] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
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;
} else if (currentState == LOW && isHigh) {
// Pin just went LOW
unsigned long duration = millis() - startTime;
totalTime += duration;
isHigh = false;
Serial.println("MACHINE TIMED OUT (OFF)");
}
// Check if the button is pressed and print the total time
if (key_pressed == '9') {
delay(250);
//lastPrintTime = millis();
Serial.print("Total Time Running: ");
Serial.println(totalTime / 1000); // Convert milliseconds to seconds
// Serial.println(" seconds");
// Add total elapsed time I Added
unsigned long elapsedMillis = millis() - lastPrintTime;
Serial.print("Total Elapsed Time: ");
Serial.println(elapsedMillis / 1000); // Convert milliseconds to seconds
// Serial.println(" seconds"); //I added for total elapsed time
Serial.print("Total part count: "); //DIsplay total count in serial monitor
Serial.println(count); //display total count in serial monitor
if (count > 0) { // parts per hour testing
float partsPerHour = float(count) / (elapsedMillis / 60000.0); // Calculate parts per hour Total ELpased TIme
Serial.print("Parts Per Minute Elapsed Time: ");
Serial.println(partsPerHour); //parts per hour testing
}
if (count > 0) { // parts per hour testing
float partsPerHour = float(count) / (totalTime / 60000); // Calculate parts per hour Total ELpased TIme
Serial.print("Parts Per Minute Running Time: ");
Serial.println(partsPerHour); //parts per hour testing
//wdt_enable(WDTO_15MS); // Enable watchdog timer (resets the Arduino)
// while (1);
}
Serial.println("END JOB");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print("JOB ENDED");
delay(10000);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(1, 1);
lcd.print("PUSH JOB START");
}
// 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);
Serial.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);
Serial.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);
Serial.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);
Serial.println("BREAK TIME"); // delete if no good
lcd.setCursor(0, 1);
lcd.print("Break Time ");
//totalTime = 0;
//lastPrintTime = millis();
//lcd.setCursor(0, 0);
//lcd.print("PART: ");
//count = 0;
}
} 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;
//lcd.setCursor(5, 0);
//lcd.print(count);
delay(500);
}
if (digitalRead(photoeyePin)) {
state = true;
delay(400);
}
}
}