help with 2 LDR sensor reading

hi… im getting this awkward reading of my LDR. i have two of it. So, i connected on A0 A1. but the reading is the same for both sensor. I closed one of the LDR using my hand and it does not have any effect. But if close the other sensor, it affect both the sensor reading. What is the problem ?
thanks…

for forum.JPG

The output tells us nothing. Supply the circuit and program.

Weedpharma

hi whedpharma…thanks for replying. take a look at this program and proteus connection

int sensorPinA0 = A0;    // pin that the Bottom/Left sensor is attached to
int sensorPinA1 = A5;    // pin that the Right sensor is attached to
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9;    // H-bridge enable pin

// variables:
  int sensorValueA0 = 0;         // Bottom/Left photoresistor
  int sensorValueA1 = 0;         // Right photoresistor
 

void setup() {
  
    // Send debugging information via the Serial monitor
  Serial.begin(9600);
  analogReference(INTERNAL);
    
    //Assign and set motor driver pins to low
    pinMode(motor1Pin, OUTPUT);
    //digitalWrite(motor1Pin, LOW);
    pinMode(motor2Pin, OUTPUT);
    //digitalWrite(motor2Pin, LOW);
    pinMode(enablePin, OUTPUT);
    //digitalWrite(enablePin, HIGH);
    //pinMode(9, OUTPUT);
    pinMode(sensorValueA0, INPUT);
    pinMode(sensorValueA1, INPUT);
    //pinMode(switchPin, INPUT);
    //digitalWrite(9, LOW);
    //pinMode(8, OUTPUT);
    //digitalWrite(8, LOW);

  }

void loop()
{
  // read the sensor:
  sensorValueA0 = analogRead(sensorPinA0);
  sensorValueA1 = analogRead(sensorPinA1);
  float voltage1 = sensorValueA0*(5.0/1023.0);
  float voltage2 = sensorValueA1*(5.0/1023.0);

  
  //sensorValueA2 = analogRead(sensorPinA2);
   // if the switch is high, motor will turn on one direction:
    if (voltage1 > voltage2)
    {
      //digitalWrite(switchPin, HIGH);  // set the switch pin high
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      digitalWrite(enablePin, HIGH);
      //delay(100);  
   }
   else
   {
      //digitalWrite(switchPin, LOW);  // set the switch pin high
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
 
    
    if (voltage1 < voltage2)
    {
      //digitalWrite(switchPin, HIGH);
      digitalWrite(motor1Pin, HIGH);
      digitalWrite(motor2Pin, LOW);
      digitalWrite(enablePin, HIGH);
    }
      else
   {
      //digitalWrite(switchPin, LOW);  // set the switch pin high
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
   
   if (voltage1 = voltage2)
    {
      //digitalWrite(switchPin, LOW);
      digitalWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, LOW);
      digitalWrite(enablePin, HIGH);
    }
      else
   {
      //digitalWrite(switchPin, LOW);  // set the switch pin high
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
 
    
  //Print debugging information
  Serial.print("left reading = ");
  Serial.println(voltage1);     // the left/bottom sensor analog reading
  Serial.print("right reading = ");
  Serial.println(voltage2);     // the right sensor analog reading
  //Serial.print("Top reading = ");
  //Serial.println(sensorValueA2);     // the top sensor analog reading
  
  delay(1000);
}

int sensorPinA0 = A0; // pin that the Bottom/Left sensor is attached to
int sensorPinA1 = A5; // pin that the Right sensor is attached to

Your original post said you are connecting to A0 and A1, your circuit suggests the same. Why is your program looking at A5?

Simplify your program by removing all but the LDR code. Get that working before adding the motor code.

Weedpharma

'else'-parts are conflicting. REWRITE

Theese can be removed

pinMode(sensorValueA0, INPUT);
pinMode(sensorValueA1, INPUT);

also

if (voltage1 = voltage2)

should be

if (voltage1 == voltage2)

weedpharma:
int sensorPinA0 = A0; // pin that the Bottom/Left sensor is attached to
int sensorPinA1 = A5; // pin that the Right sensor is attached to

Your original post said you are connecting to A0 and A1, your circuit suggests the same. Why is your program looking at A5?

Simplify your program by removing all but the LDR code. Get that working before adding the motor code.

Weedpharma

hi weedpharma. thanks for the guidance. i have done the sensor part only. and yes LDR show it values according to the light intensity. But, if i added the h-bridge code, and the comparison of voltage code, the values of LDR become the same again. what seems to be the problem ?
thanks

Have you done the corrections pointed out by other answers?

Weedpharma

yes. i've changed the = become ==, and rearranged my 'else' statement..

Please post your current code for us to check.

Weedpharma

hi weedpharma…thanks for replying. now, my sensor part is already ok. the sensor value already followed the intensity of light received. But, there is some problem occurs. The h-bridge(L293D) could not rotate my DC motor in either direction. Below i’ve attached my code. hope you can help me to find the problem. thanks…

int sensorPinA0 = A0;    // pin that the Bottom/Left sensor is attached to
int sensorPinA1 = A1;    // pin that the Right sensor is attached to
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9;    // H-bridge enable pin
int tolerance1 = 5;
int sensorValueA0 = 0;         // Bottom/Left photoresistor
int sensorValueA1 = 0;         // Right photoresistor
 
void setup() {
    Serial.begin(9600);
    //analogReference(INTERNAL);
    
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
    //pinMode(sensorValueA0, INPUT);
   // pinMode(sensorValueA1, INPUT);
  
  }

void loop()
{
  // read the sensor:
  sensorValueA0 = analogRead(sensorPinA0);
  sensorValueA1 = analogRead(sensorPinA1);
  float voltage1 = sensorValueA0*(5.0/1023.0);
  float voltage2 = sensorValueA1*(5.0/1023.0);
   if((abs(voltage1 - voltage2) <= tolerance1));
   if (voltage1 > voltage2)
    {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      digitalWrite(enablePin, HIGH); 
   }
   else
   {
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
 
    
    if (voltage1 < voltage2)
    {
      digitalWrite(motor1Pin, HIGH);
      digitalWrite(motor2Pin, LOW);
      digitalWrite(enablePin, HIGH);
    }
    else
   {
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
   
   if (voltage1 == voltage2)
    {
      digitalWrite(motor1Pin, HIGH);
      digitalWrite(motor2Pin, LOW);
      digitalWrite(enablePin, HIGH);
    }
      else
   {
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
 
    
  //Print debugging information
  Serial.print("left reading = ");
  Serial.println(voltage1);     // the left/bottom sensor analog reading
  Serial.print("right reading = ");
  Serial.println(voltage2);     // the right sensor analog reading
  delay(1000);
}

This does nothing.

if((abs(voltage1 - voltage2) <= tolerance1));

Why the ;

Add ceramic .1uf decoupling capacitors to your 328.
Add reset cctry to the controller.

http://www.atmel.com/Images/doc8278.pdf

Have you been able to drive the H bridge separately? Write a simple program to test the bridge. This will let you know if the problem is hardware or software.

Weedpharma

weedpharma:
Have you been able to drive the H bridge separately? Write a simple program to test the bridge. This will let you know if the problem is hardware or software.

Weedpharma

thanks weedpharma..yes. i already test the h-bridge seperately by using switch. and DC motor rotates. So, do i need a switch on my circuit ? it is not possible right ? because i want this to be automatically rotates based on the 'if else' statement.

That is not what he said. He said using a program not switches as the control input. There is a world of differance between the two.

Grumpy_Mike:
That is not what he said. He said using a program not switches as the control input. There is a world of differance between the two.

Grumpy_Mike:
That is not what he said. He said using a program not switches as the control input. There is a world of differance between the two.

hi grumpy_mike…thanks for make it clearly. can you give me some help here ? i’ll be very thankful if you can…

Not sure what help you need, do the test you were asked to do and post the results.
When people try and help here they are working blind if you do not tell them everything they ask. They are only asking you to do the things that they would do if they had the same problem. You are getting good advice here so it would be good to do the tests that are asked of you. Ignoring one or more just wastes time. I have seen threads that would have been solved pages ago if the OP would have done the tests asked.

If you do not understand any test you have been asked to do then ask about that.

Grumpy_Mike:
Not sure what help you need, do the test you were asked to do and post the results.
When people try and help here they are working blind if you do not tell them everything they ask. They are only asking you to do the things that they would do if they had the same problem. You are getting good advice here so it would be good to do the tests that are asked of you. Ignoring one or more just wastes time. I have seen threads that would have been solved pages ago if the OP would have done the tests asked.

If you do not understand any test you have been asked to do then ask about that.

ok. thanks.. actually i've test all those recommended. and the result is, i managed to get my sensor the actual reading for both LDR. And now, i need help on how should i program this h-bridge (with exceptions of using switch as input). Because all i understand now is to test this h-bridge whether it is working or not. Does it sound more like it ? thanks a lot for helping mate..

So now you need to run something like this:-

const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9;    // H-bridge enable pin
 
void setup() {
    Serial.begin(9600);
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
    digitalWrite(enablePin, HIGH);  //is this right for your chip?    
  }

void loop()
{
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  delay(2000);    
      digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low
  delay(2000);
}

Grumpy_mike, my h-bridge working fine and the DC motor rotates according to the programme. thanks… Now my problem is, my DC motor does not rotate when using this code.

int sensorPinA0 = A0;    // pin that the Bottom/Left sensor is attached to
int sensorPinA1 = A1;    // pin that the Right sensor is attached to
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9;    // H-bridge enable pin
int sensorValueA0 = 0;         // Bottom/Left photoresistor
int sensorValueA1 = 0;         // Right photoresistor
 
void setup() {
    Serial.begin(9600);
    //analogReference(INTERNAL);
    
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
    pinMode(sensorValueA0, INPUT);
    pinMode(sensorValueA1, INPUT);
    digitalWrite(enablePin, HIGH);
  
  }

void loop()
{
  // read the sensor:
  sensorValueA0 = analogRead(sensorPinA0);
  sensorValueA1 = analogRead(sensorPinA1);
  float voltage1 = sensorValueA0*(5.0/1023.0);
  float voltage2 = sensorValueA1*(5.0/1023.0);
   if (voltage1 > voltage2)
    {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
   }
   else
   {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
 
    
    if (voltage1 < voltage2)
    {
      digitalWrite(motor1Pin, HIGH);
      digitalWrite(motor2Pin, LOW);
    }
    else
   {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
   
   if (voltage1 == voltage2)
    {
      digitalWrite(motor1Pin, HIGH);
      digitalWrite(motor2Pin, LOW);
    }
      else
   {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
   }
 
    
  //Print debugging information
  Serial.print("left reading = ");
  Serial.println(voltage1);     // the left/bottom sensor analog reading
  Serial.print("right reading = ");
  Serial.println(voltage2);     // the right sensor analog reading
  delay(1000);
}