Running Motor for a certain amount of time whenever if-else statement triggered

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);
}
}
}

Scouring the internet won't help you, but placing Serial.print() statements in your code to monitor the values being tested and to monitor the logic flow will certainly be of help. Called "debugging" and you need to begin.

Paul

Your code use the millis() for timing incorrectly. See how to use millis() for timing.

Well, I tried different ways to use millis(). the thing is, millis() will keep going and wont stop or pause. I need it to reset everytime i go to the if statement so that the condition can be applied again for the motor

Wait, so the motor is always running? One way or the other?

Or maybe this needs to be explained a little clearer what you REALLY want.

-jim lee

ilyas_edward:
the thing is, millis() will keep going and wont stop or pause.

That's the nature of millis(), it runs continually in the background.

ilyas_edward:
I need it to reset everytime i go to the if statement

No. You don't reset millis(), you just access it. You need to follow the link in reply #2 and get to understand about timing. Maybe this will help: