#include <LiquidCrystal_I2C.h>
#include <Wire.h>
int Contrast = 75;
const int inputPin = A3; // Replace with the pin number you are using (e.g., 13 for onboard LED)
const int buttonPinA4 = 4; // Pin where the push button is connected
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 = 5; // the number of the relay pin
const int buttonPin1 = 8; // the number of the first push button pin
const int buttonPin3 = 2; // the number of the third push button pin
const int buttonPin4 = 3;
const int buttonPin5 = A2;
const int chipSelect = 10;
int count = 0;
boolean state = true;
LiquidCrystal_I2C lcd(0x27, 16, 2);
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// 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);
pinMode(buttonPinA4, INPUT_PULLUP);
pinMode(photoeyePin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP); // Internal pull-up resistor for the first button
pinMode(buttonPin3, INPUT_PULLUP); // Internal pull-up resistor for the third button
pinMode(buttonPin5, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.backlight();
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0);
lcd.print("PART: ");
lcd.setCursor(0, 1);
lcd.print("LOADING");
}
void loop(){
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 (digitalRead(buttonPinA4) == LOW) {
delay(250);
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
}
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");
// Reset the total time
//I added for total elapsed time
}
// Read the state of the photoeye sensor (digital):
int sensorValue = digitalRead(photoeyePin);
// Check if waiting for button press
if (waitForButtonPress) {
if (digitalRead(buttonPin1) == LOW) {
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 (digitalRead(buttonPin4) == LOW) {
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 (digitalRead(buttonPin3) == LOW) {
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 (digitalRead(buttonPin5) == LOW) {
waitForButtonPress = false;
lastDetectionMillis = millis(); // Reset the timer /// delete if no good to the bottom line.
digitalWrite(relayPin, HIGH);
delay(50);
Serial.println("JOB STARTED"); // delete if no good
lcd.setCursor(0, 1);
lcd.print("JOB STARTED ");
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);
}
}