#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int startButtonPin = 2; // Replace with the actual pin for the Start button
const int stopButtonPin = 3; // Replace with the actual pin for the Stop button
const int motorPinA = 9; // Replace with the actual pin for the motor control
const int motorPinB = 8; // Replace with the actual pin for the motor control
LiquidCrystal_I2C lcd(0x27, 16, 2); // Replace with your LCD's I2C address
unsigned long motorStartTime = 0;
unsigned long motorRunDuration1 = 10000; // 15 seconds in milliseconds
unsigned long motorRunDuration2 = 15000; // 10 seconds in milliseconds
unsigned long startTime; // Store the start time of the loop
unsigned long loopDuration = 10000 ; // Duration of the loop in milliseconds (24 hours)
unsigned long totalRunningTime = 0;
bool motorARunning = false;
bool motorBRunning = false;
bool isAFirstCondition = true;
bool isBFirstCondition = true;
void setup() {
Serial.begin(9600);
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
pinMode(motorPinA, OUTPUT);
pinMode(motorPinB, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor Control");
lcd.setCursor(0, 1);
lcd.print("Run Button : 1");
}
void(* resetFunc) (void) = 0;
void loop() {
unsigned long currentMillis = millis();
if (!motorARunning && digitalRead(startButtonPin) == LOW) {
motorStartTime = currentMillis;
motorARunning = true;
isAFirstCondition = true;
digitalWrite(motorPinA, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MotorA: Running");
Serial.println("MotorA: Running");
}
if (motorARunning) {
if (isAFirstCondition) {
if (currentMillis - motorStartTime >= motorRunDuration1) {
isAFirstCondition = false;
motorStartTime = currentMillis;
digitalWrite(motorPinA, !digitalRead(motorPinA)); // Toggle motor direction
motorBRunning = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MotorA: Stop");
Serial.println("MotorA: Stop");
}
} else {
if (currentMillis - motorStartTime >= motorRunDuration2) {
motorStartTime = currentMillis;
digitalWrite(motorPinB, HIGH);
motorBRunning = true; // Toggle motor direction
isBFirstCondition = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MotorB: Running");
Serial.println("MotorB: Running");
}
}
if (motorBRunning) {
if (isBFirstCondition) {
if (currentMillis - motorStartTime >= motorRunDuration1) {
isBFirstCondition = false;
motorStartTime = currentMillis;
digitalWrite(motorPinB, !digitalRead(motorPinB)); // Toggle motor direction
motorARunning = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MotorB: stop");
Serial.println("MotorB: stop");
}
}
} else {
if (currentMillis - motorStartTime >= motorRunDuration2) {
motorStartTime = currentMillis;
digitalWrite(motorPinA, HIGH);
motorARunning = true;
isAFirstCondition = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MotorA: Running");
Serial.println("MotorA: Running");
resetFunc();
}
}
unsigned long currentTime = millis(); // Get the current time
Serial.println(currentTime - startTime);
if (currentMillis - startTime >= loopDuration) {
loopDuration = currentTime ; // If 24 hours have passed, stop the loop
Serial.println("24 hours have passed. Stopping...");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("24 hours have passed. Stopping...");
}
if (digitalRead(stopButtonPin) == LOW) {
motorARunning = false;
motorBRunning = false;
digitalWrite(motorPinA, LOW);
digitalWrite(motorPinB, LOW);
totalRunningTime = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor: Stopped");
lcd.setCursor(0, 1);
lcd.print("Run Button : 1");
Serial.println("Motor: Stopped");
}
}
}