#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int inputPin = 2; // Input pin with pull-up resistor
int m_forward = 3; // Relay 1 (forward) control pin
int m_reverse = 4; // Relay 2 (reverse) control pin
long value = 40;
long menit = 60000;
unsigned long startMillis = 0;
bool timerActive = false;
unsigned long remainingTime = 0; // New variable for remaining time
const unsigned long forwardDuration = 5000; // Forward duration (in milliseconds)
const unsigned long reverseDuration = 5000; // Reverse duration (in milliseconds)
const unsigned long stopDuration = 1000; // Stop duration (in milliseconds)
bool isForward = true; // Forward/reverse status
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD with its I2C address (0x27) and dimensions (16x2)
void setup() {
Serial.begin(9600);
pinMode(inputPin, INPUT_PULLUP);
pinMode(m_forward, OUTPUT);
pinMode(m_reverse, OUTPUT);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the
lcd.setCursor(0, 0); // Set the cursor to the first row
lcd.print("TIMER MESIN CUCI"); // Display initial message
lcd.setCursor(0, 1); // Set the cursor to the first row
lcd.print(" BY ANDRI"); // Display initial message
}
void loop() {
if (digitalRead(inputPin) == LOW) {
// Input pull-up active, start the timer
startMillis = millis();
timerActive = true;
lcd.clear();
Serial.println("CUCI DIMULAI!");
lcd.setCursor(1, 0); // Set the cursor to the first row
lcd.print("CUCI DIMULAI!!"); // Display initial message
}
if (timerActive) {
unsigned long currentMillis = millis();
remainingTime = menit * value - (currentMillis - startMillis); // Calculate remaining time
lcd.setCursor(0, 1); // Set cursor to the second row
lcd.print("Waktu: " + String(remainingTime / 60000) + "Mnt " + String((remainingTime % 60000) / 1000)+ "dtk ");
Serial.print("Waktu: " + String(remainingTime / 60000) + ":" + String((remainingTime % 60000) / 1000));
if (currentMillis - startMillis >= (menit)*(value)) {
// Timer ends
timerActive = false;
digitalWrite(m_forward, LOW); // Turn off relay 1
digitalWrite(m_reverse, LOW); // Turn off relay 2
Serial.println("NYUCINYA UDAH SELESAI BANG!!");
lcd.clear();
lcd.setCursor(0, 0); // Set the cursor to the first row
lcd.print(" CUCI SELESAI!!"); // Display initial message
} else {
// During the timer, alternate between relays
if (isForward && currentMillis - startMillis < forwardDuration) {
digitalWrite(m_forward, HIGH);
digitalWrite(m_reverse, LOW);
}
// Reverse for 5 seconds after moving forward
else if (!isForward && currentMillis - startMillis < reverseDuration) {
digitalWrite(m_forward, LOW);
digitalWrite(m_reverse, HIGH);
}
// Stop for 1 second before changing direction
else if (currentMillis - startMillis < forwardDuration + stopDuration) {
digitalWrite(m_forward, LOW);
digitalWrite(m_reverse, LOW);
}
// Switch direction after completing the duration
else {
isForward = !isForward;
startMillis = currentMillis; // Reset start time
digitalWrite(m_forward, LOW);
digitalWrite(m_reverse, LOW);
}
}
}
// Print remaining time on the LCD
}