Newbie requiring help with conditional

Hi all,
I’ve recently started programming and I need help with understanding code.
I have been tasked to write code so that a motor will turn on when an LDR value is above 75% and turn in the opposite direction for 1 second when the value is less than 25%. The code works fine to turn the motor in the directions, however I don’t understand how to tell the motor to only turn for one second and then wait until the value is above 75% again.
From what I can understand I need to use a conditional statement such as ‘while()’ ?
Thanks

int enable1 = 11;	// Define pins used by L293D
int input1 = 10;
int input2 = 9;	
const int spd = 70;	// Define variable for motor direction
int LDRval = A0;
int x;
void setup() {
  Serial.begin(9600);
  pinMode(enable1, OUTPUT);	// Define pinModes
  pinMode(input1, OUTPUT);
  pinMode(input2, OUTPUT); 
  digitalWrite(enable1, LOW);	// Motor is initially off
  digitalWrite(input1, LOW);
  digitalWrite(input2, LOW);
  
}
void loop() {
  LDRval = analogRead (A0);	// Read LDR value
  Serial.println (LDRval);	// Print LDR value
  delay(500);
   analogWrite(enable1, spd);
  delay(1000);
    if (LDRval < 25)
  {
    digitalWrite(input1, LOW);
    digitalWrite(input2, HIGH);
  }
  else if ((LDRval > 25) && (LDRval <= 75))
  {
    digitalWrite(input1, LOW);
    digitalWrite(input2, LOW);
  }
  else
  {
    digitalWrite(input1, HIGH);
    digitalWrite(input2, LOW);
  }
}

Have a look at Using millis for timing.

If you look at what analogRead() returns as a value analogRead you will notice it returns a value between 0 and 1023.

25% of that number is not the value 25. Same for 75. A little math is in order.

Thanks blh64, however I am using a resistor in the circuit that makes the output of the LDR between 0 and 100. This is to keep the code as simple as possible

I don’t understand how to tell the motor to only turn for one second and then wait until the value is above 75% again.

You can combine a millis() based timing cycle for the motor with state change code for one time actions based on the analog values. You can do that with either a state machine or conditional tests using boolean control variables.

Here is some example code using boolean control variables to turn on a timed output once after an analog threshold is crossed. I have placed the analogRead() on a 5 second timer, and the low ldr value cycle is re enabled after the reading goes high. You should be able to adapt this to your situation.

int sensorPin = A1; // ldr connected to A1
int sensorValue; // variable to store the value coming from the ldr
int testLED = 13; //indicator 
boolean motorRunTiming = false;
boolean lowLdrAction = false;
unsigned long startTime;
unsigned long onTime = 1000;
unsigned long analogReadInterval = 5000;
unsigned long lastTimeRead = millis();

void setup()
{
  Serial.begin(9600); // initialize the serial port
  pinMode(testLED, OUTPUT); // sets digital pin 9 to be an output
}

void loop()
{
  if (millis() - lastTimeRead >= analogReadInterval)//read ldr periodically
  {
    lastTimeRead = millis();
    sensorValue = analogRead(sensorPin); // read the value from the voltage sensor
    Serial.println(sensorValue);
  }

  if (sensorValue <= 25 && lowLdrAction == false)
  {
    lowLdrAction = true;//do low Ldr actions only once
    motorRunTiming = true; //start motor on timing
    startTime = millis();
    //setMotor direction and turn motor on
    digitalWrite(testLED, HIGH); //turns on led connected to digital pin 9
  }

  if (motorRunTiming ==true && millis() - startTime >= onTime)
  {
    //turn motor off
    digitalWrite(testLED, LOW);
    motorRunTiming = false;
  }

  if (sensorValue >= 75 && lowLdrAction == true)
  {
    lowLdrAction = false;//reset for next low
    motorRunTiming = true; //start motor on timing
    startTime = millis();
    //setMotor direction and turn motor on
    digitalWrite(testLED, HIGH); //turns on led connected to digital pin 9
  }
}