#include <LiquidCrystal.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 = A4; // Pin where the push button is connected
unsigned long totalTime = 0;
unsigned long startTime = 0;
bool isHigh = false;
const unsigned long printInterval = 10000; // 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
const int buttonPin2 = 9; // the number of the second push button pin
const int buttonPin3 = 10; // the number of the third push button pin
const int buttonPin4 = A5;
const int buttonPin5 = A2;
int count = 0;
boolean state = true;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// variables for photoeye sensor
unsigned long lastDetectionMillis = 0;
const long interval = 10000; // 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(buttonPin2, INPUT_PULLUP); // Internal pull-up resistor for the second button
pinMode(buttonPin3, INPUT_PULLUP); // Internal pull-up resistor for the third button
pinMode(buttonPin5, INPUT_PULLUP);
analogWrite(6, Contrast);
lcd.begin(16, 2);
pinMode(photoeyePin, INPUT);
lcd.setCursor(0, 0);
lcd.print("Count No: ");
lcd.setCursor(0, 2);
lcd.print("LOADING");
Serial.begin(9600);
}
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;
}
// Check if the button is pressed and print the total time
if (digitalRead(buttonPinA4) == LOW) {
delay(250);
Serial.print("Total Time Running ");
Serial.print(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.print(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");
// 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("Machine Down");
lcd.setCursor(0, 1);
lcd.print("Ready ");
} else if (digitalRead(buttonPin2) == LOW) {
waitForButtonPress = false; // Button has been pressed, reset the flag
lastDetectionMillis = millis(); // Reset the timer
digitalWrite(relayPin, HIGH);
delay(50);
Serial.println("Break Time");
lcd.setCursor(0, 1);
lcd.print("RUNNING ");
} 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("Machine Attachment Down");
lcd.setCursor(0, 1);
lcd.print("RUNNING ");
} 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("SHEET CHANGE");
lcd.setCursor(0, 1);
lcd.print("RUNNING ");
} 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("Count No: ");
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("PUSH A BUTTON?");
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(11, 0);
lcd.print(count);
delay(500);
}
if (digitalRead(photoeyePin)) {
state = true;
delay(400);
}
}