DC Motor Control using a photoresistor/ldr (Help/Teach me)

Good day to everyone,

I have been using a ldr to control a dc motor via arduino uno.
The problem is that i just want to run the dc motor for a specific amount of time (e.g. 6 seconds)
after the my ldr input has read "HIGH". It will be a great help if someone will teach me or help me.
for more infos about my project here's a link of my circuit. Thank you in advance to the community.

int ldr = 8;
int mot = 4;

void setup() {
 
  pinMode(mot, OUTPUT);
  pinMode(ldr, INPUT);
  
}

void loop(){
  digitalWrite(mot, LOW);
  if(digitalRead(ldr)==HIGH)
    
  {
    
    digitalWrite(mot, HIGH);
    delay(1000);
    
    
  }
}

dcmotorcontrolvialdr.ino (240 Bytes)

I can't see your circuit because it wants me to sign in.

I also can't see anything in your code that seems to be trying to run a motor for 6 seconds? What does it actually do now?

Steve

sorry for the confusion. Currently my dc motor in my circuit runs continuously when the input in my ldr is high (when there is light). My goal is when my ldr reads high or when there is light the dc motor will only run for only 6 seconds and then stop.

When the LDR input changes from LOW to HIGH you need to save the value of millis() and start the motor.

Then your code can check

if (millis() - motorStartMillis >= 6000) {
   // code to stop motor
}

Have a look at how millis() is used to manage timing in Several things at a time.

...R

Thanks you very much.