#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 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 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");
}
}
unsigned long elapsedTime = currentMillis - motorStartTime;
if (elapsedTime >= 86400000) { // 24 hours in milliseconds 86400000
motorBRunning = false;
digitalWrite(motorPinA, LOW);
digitalWrite(motorPinB, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor: Stopped");
Serial.println("Motor: Stopped");
}
lcd.setCursor(0, 1);
lcd.print("Time : ");
lcd.print(elapsedTime / 1000); // Convert to seconds
lcd.print(" sec");
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");
}
}
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");
}
}
unsigned long elapsedTime = currentMillis - motorStartTime;
if (elapsedTime >= 86400000) { // 24 hours in milliseconds
motorBRunning = false;
digitalWrite(motorPinA, LOW);
digitalWrite(motorPinB, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor: Stopped");
Serial.println("Motor: Stopped");
}
lcd.setCursor(0, 1);
lcd.print("Time : ");
lcd.print(elapsedTime / 1000); // Convert to seconds
lcd.print(" sec");
}
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");
}
}