comparing two LDR sensor to enable/disable SwitchPin for H-bridge

hi there, im going to do a project which compared two LDR sensor to drive DC motor rotation. and yes im using h-bridge to control them. So, im using 'if else' statement for the programmed, but still no result can be seen on the DC motor rotation. What im trying to do is, im comparing the voltage differences between both LDR. example, when LDR 1 output voltage is higher than LDR 2, the rotation move clockwise and, vice versa. am i on the right pathway ? thanks for helping.. :slight_smile:

Where is your code?

Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

Weedpharma

thanks for replying. this the code that i've been working..

Take the semicolon off the end of the if lines

Please post your code properly and not as a screenshot. Read the Forum how-to

You should also provide a diagram showing how the LDRs are wired. The best diagram is a photo of a pencil drawing. A photo of the LDRs themselves will probably be useless.

...R

here is my code :slight_smile:

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
const int switchPin = 2;

  int sensorValueA0 = analogRead(sensorPinA0);
  int sensorValueA1 = analogRead(sensorPinA1);

void setup() {
  
     Serial.begin(9600);
  analogReference(INTERNAL);
    
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
    digitalWrite(enablePin, HIGH);
    pinMode(sensorValueA0, INPUT);
    pinMode(sensorValueA1, INPUT);
    pinMode(switchPin, 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 the voltage1 > voltage2, 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
      //delay(100);  
  }
    
    // if the switch is low, motor will turn in the other direction:
    else 
    {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(100);  
    }
 
    if (voltage1 == voltage2)
    {
      digitalRead(switchPin, HIGH);
      digitalWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, LOW);
      delay(100);  
    }
   
  //Print debugging information
  Serial.print("sensorPinA0 = ");
  Serial.println(voltage1);     // the left/bottom sensor analog reading
  Serial.print("sensorPinA1 = ");
  Serial.println(voltage2);     // the right sensor analog reading
   
  delay(1000);
}

this is my circuit. what can be use for a switch to the h-bridge ? because in other people projects, they have the switch to control the h-bridge. But in this project im using LDR as the input.

i could not upload my drawing. it says 'security check fails' what happen..

this is the proteus file version

If you are using float values you will be very unlikely to get two identical values so the == test is not much use.

What are your print statements showing you?

What happens if you temporarily replace the ADC readings with fixed values - such as

sensorValueA0 = 100;
  sensorValueA1 = 500;

...R

    if (voltage1 > voltage2);

Does exactly nothing, since the only thing the if controls is the empty statement
represented by the semicolon. This is how to use if:

  // for single statement:
  if (a < b)
    do_something ;

  // or for a block
  if (a < b)
  {
    do_several () ;
    things () ;
  }

  ; // this is an empty statement!

because im using h-bridge to control DC motor, to turn this DC motor there must be some kind of switch. but here, im using a pair of LDR to trigger this switch. So, what should i put as a switch here ? some kind of relays or mosfet ? thanks for your help. :slight_smile:

MarkT is correct, you need to get rid of the semicolon at the end of your "if" statement. Also, it's not wrong but your converting the values to voltage does nothing for you functionally. You can just as easily compare "counts" in your code.

Apart from that, it appears your hardware is all there. You talk of a SWITCH, is this just so you can turn the system off? If so, just grab an input, enable it as INPUT_PULLUP and let your pushbutton or toggle switch ground the pin. Then add the code for the switch.

A simple way to try your code is simply short out your LDR's one at a time which should simulate max voltage to your analog inputs.