DC motor control with LDR

Hi, first time posting so let me know of any mistakes.

I'm doing a simple project which requires an LDR to move a DC motor using a L293D. If the light level is below 25% the motor is to move in one direction for one second, and if the light level is above 75% the motor will move in the other direction.
I have tried to write a code but it is not working, any advice would be appreciated.

int enable1 = 11;  // Define pins used by L293D
int input1 = 10;
int input2 = 9;
int dir;  // Define variable for motor direction and speed
int spd;
int LDRval = A0;
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);
  spd = 70;
  if (LDRval >= 75)
  {digitalWrite(input1, HIGH);
   digitalWrite(input2, LOW);}
  else if ((LDRval < 75) && (LDRval > 25))
  {digitalWrite(input1, LOW);
   digitalWrite(input2, LOW);}
  else
  {digitalWrite(input1, LOW);
  digitalWrite(input2, HIGH);}
}

LDR_Motor_Control.ino (888 Bytes)


OP image. How to post an image.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code. Many members will not download code.

What is the value of the load resistor in the LDR voltage divider? If it is really 55 ohms, it seems way too low a value.

9V smoke alarm batteries do not have the capacity to run a motor for long, if at all. What are the rated voltage and stall current specs of the motor?

The L293 motor driver is ancient and inefficient technology, ill suited to battery operation cause the loose so much energy to heat. Pololu has a wide selection of modern MOSFET output motor drivers at reasonable prices.

 if (LDRval <75)  // @@@@@@@@@  should this be >= 75?
  {
    digitalWrite(input1, HIGH);
    digitalWrite(input2, LOW);
  }
  else if ((LDRval < 75) && (LDRval > 25))

If the LDRvalue is less than 75 the first if will always execute and the else if will never execute.

I'm just using different resistors for the LDR at the moment, the only reason I have a 55 Ohm resistor is that the output value for the LDR is then between 0 and 100, meaning that I don't have to use any mapping function.

The project is for college and it is closed now until next year so I am stuck using TinkerCad for the project. The L293D was the power control I was told to use but if you know of any better ones let me know.

Thanks

And yes, sorry that value is supposed to the >= 75

julianrd:
I'm just using different resistors for the LDR at the moment, the only reason I have a 55 Ohm resistor is that the output value for the LDR is then between 0 and 100, meaning that I don't have to use any mapping function.

Even if the LDR output is in the range (for example) 183 to 657 there would be no need for the map() function. The difference between the two numbers is 474. 25% of that is 118 so check whether the value is below 301 or above 539.

...R