//Initial_RTC
#include <RTClib.h>
RTC_DS3231 rtc;
long duration;
int distance;
char daysOfTheWeek[7][12] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
//Initial_Joystick
#define SEL_PIN 7
//Initial_Distance Sensor-HC-SR04
#define trigPin 2
#define echoPin 4
//Inital_Potentiometer
float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//Time Variables
unsigned long startTime3; // Variabel untuk menyimpan waktu mulai
unsigned long WorkingTime; // Variabel untuk menyimpan Working-Time
unsigned long startTime; // Variabel untuk menyimpan waktu mulai
unsigned long TresholdTime; // Variabel untuk menyimpan Treshold Time
unsigned long startTime2; // Variabel untuk menyimpan waktu mulai
unsigned long DownTime; // Variabel untuk menyimpan Down-Time
unsigned long TotalDownTime; // Variabel untuk menyimpan Down-Time
bool timerRunning3 = false; // Flag untuk mengecek apakah timer sedang berjalan
bool timerRunning = false; // Flag untuk mengecek apakah timer sedang berjalan
bool timerRunning2 = false; // Flag untuk mengecek apakah timer sedang berjalan
int lastC = 1; // Variabel untuk menyimpan nilai C sebelumnya
int lastD = 1;
int lastE = 1;
//Main Variables
int A;
int B;
int C;
int D;
int E;
void setup() {
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
// SETUP_Distance Sensor - HC-SR04
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// SETUP_RTC-MODULE
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1);
}
// automatically sets the RTC to the date & time on PC this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// manually sets the RTC with an explicit date & time, for example to set
// January 21, 2021 at 3am you would call:
// rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0));
//Setup_Joystick
pinMode(SEL_PIN, INPUT_PULLUP);
}
void loop() {
// Ultrasonic Distance Sensor - HC-SR04 ((Variabel C))
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
//Time keeping RTC Module
DateTime now = rtc.now();
// Main Variabel Definition
//Variabel A (Waktu Kerja)
if (7 >= (now.hour()+7, DEC) < 16) {
A = 1;
} else {
A = 0;
}
// Variabel B (Arus Mesin)
int analogValue = analogRead(A3);
// Rescale to potentiometer's voltage (from 0V to 5V):
float voltage = floatMap(analogValue, 0, 1023, 0, 5);
if (analogValue==0) {
B = 0;
} else {
B = 1;
}
//Variabel C (Pos Raw Material)
if (distance > 200) {
C = 1;
} else {
C = 0;
}
// Variabel D (Beban Cutter)
if (analogValue>=500) {
D = 1;
} else {
D = 0;
}
// Variabel E (Finish-Process) Joystick
bool selPressed = digitalRead(SEL_PIN) == LOW;
// horz goes from 0 (right) to 1023 (left)
// vert goes from 0 (bottom) to 1023 (top)
// selPressed is true is the joystick is pressed
if (selPressed == true) {
E = 1 ;
} else {
E = 0;
}
//WORKING-TIME SETUP
// Memulai Working Time
if ( A == 1 && !timerRunning3) {
startTime3 = millis(); // Menyimpan waktu mulai
timerRunning3 = true; // Menandai bahwa timer telah dimulai
WorkingTime = 0; // Reset elapsed time
}
// Menghitung waktu yang berlalu jika timer sedang berjalan
if (timerRunning3) {
WorkingTime = (millis() - startTime3)/1000;
}
//TRESHOLD-TIME SETUP
// First Time Treshold-Time
if (WorkingTime <= 30 && C == 0 && lastC == 1) {
startTime = millis(); // Menyimpan waktu mulai
timerRunning = true; // Menandai bahwa timer telah dimulai
TresholdTime = 0; // Reset elapsed time
}
// On Looping Treshold-Time
if (WorkingTime > 30 && C == 0 && lastC == 1 && D == 0 && lastD == 1 && E == 0 && lastE == 1 && !timerRunning2 ){
startTime = millis(); // Menyimpan waktu mulai
timerRunning = true; // Menandai bahwa timer telah dimulai
TresholdTime = 0; // Reset elapsed time
}
// Menghitung waktu yang berlalu jika timer sedang berjalan
if (timerRunning) {
TresholdTime = (millis() - startTime)/1000;
}
lastC = C;
lastD = D;
lastE = E;
//TT pada kriteria#1
if (A == 0) {
timerRunning = false; // Menandai bahwa timer telah dimulai
TresholdTime = 0; // Reset elapsed time
}
//TT pada kriteria#2
if (B == 0) {
timerRunning = false; // Menandai bahwa timer telah dimulai
TresholdTime = 0; // Reset elapsed time
}
// Saat treshold time lebih dari 30 timer off
if (TresholdTime > 30) {
timerRunning = false;
TresholdTime = 0;
}
//DOWN-TIME SETUP
// Memulai downtime pada kriteria 1 (Waktu Kerja)
if (A == 0 && !timerRunning2) {
startTime2 = millis(); // Menyimpan waktu mulai
timerRunning2 = true; // Menandai bahwa timer telah dimulai
DownTime = 0; // Reset elapsed time
}
// Memulai downtime pada kriteria 2 (Arus Mesin)
if (B == 0 && !timerRunning2) {
startTime2 = millis(); // Menyimpan waktu mulai
timerRunning2 = true; // Menandai bahwa timer telah dimulai
DownTime = 0; // Reset elapsed time
}
// Memulai downtime pada kriteria 3 (Pos Raw Material)
if (TresholdTime > 29 && !timerRunning2) {
startTime2 = millis(); // Menyimpan waktu mulai
timerRunning2 = true; // Menandai bahwa timer telah dimulai
DownTime = 0; // Reset elapsed time
}
// Menghitung waktu yang berlalu jika timer sedang berjalan
if (timerRunning2) {
DownTime = (millis() - startTime2)/1000;
// Menghentikan Downtime saat one cycle dilakukan
if (C == 0 && lastC == 1 && D == 0 && lastD == 1 && E == 0 && lastE == 1 && !timerRunning ){
startTime = millis(); // Menyimpan waktu mulai
timerRunning = true; // Menandai bahwa timer telah dimulai
TresholdTime = 0; // Reset elapsed time
}
// Menghitung waktu yang berlalu jika timer sedang berjalan
if (timerRunning) {
TresholdTime = (millis() - startTime)/1000;
}
}
lastC = C;
lastD = D;
lastE = E;
// Serial Monitor Printing (Ctrl+Shift+M):
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print("(");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") | ");
Serial.print(now.hour()+7, DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(" A=");
Serial.print(A);
Serial.print(" B=");
Serial.print(B);
Serial.print(" C=");
Serial.print(C);
Serial.print(" D=");
Serial.print(D);
Serial.print(" E=");
Serial.print(E);
// Menampilkan waktu yang berlalu di Serial Monitor jika timer sedang berjalan
if (timerRunning3) {
Serial.print(" W/T: ");
Serial.print(WorkingTime);
Serial.println(" s");
}
// Total Downtime
Serial.print(" Tud: ");
Serial.print(TotalDownTime);
Serial.println(" s");
// Menampilkan waktu yang berlalu di Serial Monitor jika timer sedang berjalan
if (timerRunning) {
Serial.print(" | T/T: ");
Serial.print(TresholdTime);
Serial.println(" s");
}
// Menampilkan waktu yang berlalu di Serial Monitor jika timer sedang berjalan
if (timerRunning2) {
Serial.print(" D/T: ");
Serial.print(DownTime);
Serial.println(" s");
}
delay(1000);
}