When the LDR hits a certain number spin motor for 10 seconds

hi so i can get my code to run a motor in my case just switched it out for an led when the LDR hits 900 however i want to stop it running after 10 seconds how could i do this?

my code is not good but i just dont know what to do. we did this millis thing in class and i thought that would help but idk

type or paste cint LED =10;
int motorLED=9;
long starttime = 0;  
long currenttime = 0;
long duration = 10000;

void setup()
{
 Serial.begin(9600);
  pinMode(LED,OUTPUT);
  pinMode(A0, INPUT);
  pinMode(motorLED,OUTPUT);
}

void loop()
{
 int sensorvalue = analogRead(A0);
  Serial.println(sensorvalue);
  delay(5);
  if (sensorvalue>=900)
  {
    starttime=millis();
    digitalWrite(LED,255);
    digitalWrite(motorLED,HIGH);
    
  }
  currenttime=millis();
 if(currenttime-starttime>=duration)
 {
   digitalWrite(motorLED,LOW);
   delay(10000);
 }
   
 
    else
    {digitalWrite(LED,0);
    }
  
}

Welcome to the forum

Good or bad it is better if you post it in the most helpful way

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

my bad cheers

What if the analog reading is >900 after 10 seconds?

then it just keeps the light on this is my problem because it is supposed to represent night time it will keep the light on as its always above 900

Are you saying that the value of the LDR when read by the AD is not dropping below 900 and that is the issue?

Basically its an automatic blind and light system when it gets dark enough the lights will turn on and the motor will turn for x time then stop. Problem is because its in an if LRD >=900 which it will be for hours because its night time the the motor will always run because iys it keeps looping the if

You need to detect when the value becomes greater than the trigger level rather than when it is above the trigger level

See the StateChangeDetection example in the IDE

When a light is shined onto the LDR what is the value?

When the LDR is covered what is the value?

Have you tried using this thing

and words like "window blind" to see how many many other people have done the thing?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.