Hey guys, im doing a project where the DC motor is triggered for 3 seconds every time the condition in the if statement is met. But if the else statement is met, the DC motor will trigger for another 3 seconds but this time in the opposite direction. I've figured out how to run the motor in both directions but it just keeps on running forever. I just want it to work for 3 seconds every time if or else statement is met.
I found out a code for making it work for 3 seconds but after that it completely stops working. So meaning it only works once. I want it to work multiple times. Ive been scouring the internet for the past 2 days and came up blank. Any advices?
Below is the code i used. Im using Proteus and arduino for an LDR sensor.
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int start;
int start2;
int originalMillis = 100000;
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
lcd.setCursor(0, 0);
lcd.print("LDR Output = ");
lcd.setCursor(0, 1);
lcd.print("LDR Sensor Working");
pinMode(0, OUTPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
int ldr = analogRead(A0);
lcd.setCursor(13, 0);
lcd.print(ldr);
if (ldr >= 200) {
start = millis();
lcd.setCursor(0, 2);
lcd.print("Curtains Closed");
digitalWrite(0, HIGH);
digitalWrite(8, HIGH);
if (start <= originalMillis) {
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
}
else {
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
}
else {
start2 = millis();
lcd.setCursor(0, 2);
lcd.print("Curtains Opened");
digitalWrite(0, LOW);
digitalWrite(8, LOW);
if (start2 <= originalMillis) {
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
else {
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
}
}