4 LDRs + 2 DC MOTORS + 2 DROK L298N Motor Drive Board DC Dual H-Bridge

hi, i am having trouble with my dc motor turning directions.

my code works, it’s just when I cover up the left and right set of LDRs at two separate instances the motor still spins in the same direction.

it spins only clockwise and not counter clockwise. any ideas why?

i used code from this site to make the motor go forward and reverse:

and I modified user Spellmal’s code in the link below to incorporate the function of the 4 LDRs with respect to two motors

// connect motor controller pins to Arduino digital pins
// motor one, azimuth adjustment
int enA = 5;
int in1 = 6;
int in2 = 7;
// motor two, elevation adjustment
int enB = 8;
int in3 = 9;
int in4 = 10;

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;

//LDR top right
int topright = 0;

//LDR top left 
int topleft = 1; 

//LDR bottom left
int bottomleft = 2; 

//LDR bottom right
int bottomright = 3; 

//delay time in milliseconds
int dtime = 20; 

//range of tolerance between LDR readings
int tolerance = 100;

//start millisecond count of LDR readings
int count = 0; 

void setup() 
{
 // initialize the serial port:
 Serial.begin(9600); 
 // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}
void loop()
{
 int tr = analogRead(topleft);
 int tl = analogRead(topright);
 int bl = analogRead(bottomleft);
 int br = analogRead(bottomright); 
//print resistance values to serial monitor for debugging
  Serial.println(tr);
  Serial.println(tl);
  Serial.println(bl);
  Serial.println(br);
  Serial.println();
//incremental count increase, continues to show LDR results
  count++;

  int avt= (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  int avgTot = (tr+tl+bl+br)/4;

  int dvert = avt - avd; // check the difference of top and bottom LDRs
  int dhoriz = avl - avr;// check the difference of left and right LDRs

if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
  if (avt > avd) //if average resistance on top is greater than on bottom
  {
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   // set speed out of possible range 0~255
   analogWrite(enB, 10);
  }
  else //avg resistance on bottom is greater than on top
  {
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // set speed out of possible range 0~255
   analogWrite(enB, 10); 
  }
}
  else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
  {
   digitalWrite(in3, LOW);
   digitalWrite(in4, LOW);
  }

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
  if (avl > avr)
  {
   digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   // set speed to 200 out of possible range 0~255
   analogWrite(enA, 200); 
  }
  else
  {
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   // set speed to 200 out of possible range 0~255
   analogWrite(enA, 200); 
  }
}
  else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
  {
   digitalWrite(in1, LOW);
   digitalWrite(in2, LOW);
  }
  delay(dtime);
}
if (-1*tolerance > dvert || dvert > tolerance)

abs() is a very useful function/macro.

What, if anything, so the debug prints tell you about the robot’s behaviour?

In some of the cases you write to the enable pins whilst in others you don't. Is that correct ?

Can I suggest that you separate the code to set the motor speeds and and directions into functions with meaningful names so that the intention of your code is clearer.

For instance

   if (avt > avd) //if average resistance on top is greater than on bottom
    {
       turnRight();
    }

with a function like this

void turnRight()
{
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
      // set speed out of possible range 0~255
      analogWrite(enB, 10);
}

If required you could pass the speed to the function as a parameter.

Using functions it will also be much easier to test them individually just by calling them.

hi, in all the cases I enable all pins simultaneously.
the solar tracker is suppose to move horizontally and vertically simultaneously.

i figured out what was the issue and the program works now. thanks.
it was the pulsewidth modulation pins in the arduino uno.
i had them numbered wrong.
now all of the pins for high/low are pwm pins.

just for some food for thought i did modify the code like you suggested.
i see how it will be useful for individually testing the program for motor direction and changing settings.

the code is written below. however, nothing moves now.
i am guessing it’s because the void function works only once.
i tried putting the void functions in the void loop function but i got errors.
should i move it elsewhere?

// connect motor controller pins to Arduino digital pins
// pins on arduino uno for pwm (pulse width modulation) are 3,5,6,9,10,11
// motor one, azimuth adjustment
int enA = 7;
int in1 = 5;
int in2 = 6;
// motor two, elevation adjustment
int enB = 8;
int in3 = 9;
int in4 = 10;

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;

//LDR top right
int topright = 0;

//LDR top left 
int topleft = 1; 

//LDR bottom left
int bottomleft = 2; 

//LDR bottom right
int bottomright = 3; 

//delay time in milliseconds
int dtime = 20; 

//range of tolerance between LDR readings
int tolerance = 100;

//start millisecond count of LDR readings
int count = 0; 

void setup() 
{
 // initialize the serial port:
 Serial.begin(9600); 
 // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void turnclockwise()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed out of possible range 0~255
  analogWrite(enA, 100); 
}

void turncounterclockwise()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // set speed out of possible range 0~255
  analogWrite(enA, 100); 
}

void horizontalstop()
{
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
}

void turnup()
{
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed out of possible range 0~255
  analogWrite(enB, 10);
}

void turndown()
{
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // set speed out of possible range 0~255
   analogWrite(enB, 10);
}

void verticalstop()
{
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void loop()
{
 int tr = analogRead(topleft);
 int tl = analogRead(topright);
 int bl = analogRead(bottomleft);
 int br = analogRead(bottomright); 
//print resistance values to serial monitor for debugging
  Serial.println(tr);
  Serial.println(tl);
  Serial.println(bl);
  Serial.println(br);
  Serial.println();
//incremental count increase, continues to show LDR results
  count++;

  int avt= (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  int avgTot = (tr+tl+bl+br)/4;

  int dvert = avt - avd; // check the difference of top and bottom LDRs
  int dhoriz = avl - avr;// check the difference of left and right LDRs


if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
  if (avt > avd) //if average resistance on top is greater than on bottom
  {
  turnup();
  }
  else //avg resistance on bottom is greater than on top
  {
  turndown(); 
  }
}
  else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
  {
  verticalstop();
  }

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
  if (avl > avr)
  {
  void turnclockwise();
  }
  else
  {
  turncounterclockwise();
  }
}
  else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
  {
  horizontalstop(); 
  }
  delay(dtime);
}

but i got errors

and I couldn't be arsed to post them.

abs() is really, really useful

i am guessing it's because the void function works only once.

Which function are you referring to ?

@groove :slight_smile:

sorry but i wasn’t sure how to implement the abs() function.
i figured since my code is working i didn’t want to risk messing it up.

would it work like this??.. i put it into my code and it compiles

if (-1*tolerance > dvert || dvert > tolerance)

//changed to

if (abs(tolerance) < dvert )

@ukhelibob

i wrote all these in the new revision of code and placed them right after void setup.
the code compiles, but doesn’t compile if i place them in the void loop.
basically for example when i place turnclockwise(); in my void loop the if statement it’s not cycling through and the motors don’t spin.

if (avl > avr)
{
turnclockwise();
}

void turnclockwise()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed out of possible range 0~255
analogWrite(enA, 100);
}

void turncounterclockwise()
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// set speed out of possible range 0~255
analogWrite(enA, 100);
}

void horizontalstop()
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}

void turnup()
{
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed out of possible range 0~255
analogWrite(enB, 10);
}

void turndown()
{
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// set speed out of possible range 0~255
analogWrite(enB, 10);
}

void verticalstop()
{
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

// connect motor controller pins to Arduino digital pins
// pins on arduino uno for pwm (pulse width modulation) are 3,5,6,9,10,11
// motor one, azimuth adjustment
int enA = 7;
int in1 = 5;
int in2 = 6;
// motor two, elevation adjustment
int enB = 8;
int in3 = 9;
int in4 = 10;

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;

//LDR top right
int topright = 0;

//LDR top left 
int topleft = 1; 

//LDR bottom left
int bottomleft = 2; 

//LDR bottom right
int bottomright = 3; 

//delay time in milliseconds
int dtime = 20; 

//range of tolerance between LDR readings
int tolerance = 100;

//start millisecond count of LDR readings
int count = 0; 

void setup() 
{
 // initialize the serial port:
 Serial.begin(9600); 
 // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void turnclockwise()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed out of possible range 0~255
  analogWrite(enA, 100); 
}

void turncounterclockwise()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // set speed out of possible range 0~255
  analogWrite(enA, 100); 
}

void horizontalstop()
{
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
}

void turnup()
{
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed out of possible range 0~255
  analogWrite(enB, 10);
}

void turndown()
{
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // set speed out of possible range 0~255
   analogWrite(enB, 10);
}

void verticalstop()
{
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void loop()
{
 int tr = analogRead(topleft);
 int tl = analogRead(topright);
 int bl = analogRead(bottomleft);
 int br = analogRead(bottomright); 
//print resistance values to serial monitor for debugging
  Serial.println(tr);
  Serial.println(tl);
  Serial.println(bl);
  Serial.println(br);
  Serial.println();
//incremental count increase, continues to show LDR results
  count++;

  int avt= (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  int avgTot = (tr+tl+bl+br)/4;

  int dvert = avt - avd; // check the difference of top and bottom LDRs
  int dhoriz = avl - avr;// check the difference of left and right LDRs


if (abs(tolerance) < dvert) // check if the difference is in the tolerance else change vertical angle
{
  if (avt > avd) //if average resistance on top is greater than on bottom
  {
  turnup();
  }
  else //avg resistance on bottom is greater than on top
  {
  turndown(); 
  }
}
  else if (abs(tolerance) > dvert) //if difference is smaller than tolerance, stop vertical stepper
  {
  verticalstop();
  }

if (abs(tolerance) < dhoriz) // check if the difference is in the tolerance else change horizontal angle
{
  if (avl > avr)
  {
  turnclockwise();
  }
  else
  {
  turncounterclockwise();
  }
}
  else if (abs(tolerance) > dhoriz) //if difference in horizontal
  {
  horizontalstop(); 
  }
  delay(dtime);
}

@groove

with regards to the abs() function

code that is working… not using abs() macro here…

// connect motor controller pins to Arduino digital pins
// pins on arduino uno for pwm (pulse width modulation) are 3,5,6,9,10,11
// motor one, azimuth adjustment
int enA = 7;
int in1 = 5;
int in2 = 6;
// motor two, elevation adjustment
int enB = 8;
int in3 = 9;
int in4 = 10;

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;

//LDR top right
int topright = 0;

//LDR top left 
int topleft = 1; 

//LDR bottom left
int bottomleft = 2; 

//LDR bottom right
int bottomright = 3; 

//delay time in milliseconds
int dtime = 20; 

//range of tolerance between LDR readings
int tolerance = 100;

//start millisecond count of LDR readings
int count = 0; 

void setup() 
{
 // initialize the serial port:
 Serial.begin(9600); 
 // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}
void loop()
{
 int tr = analogRead(topleft);
 int tl = analogRead(topright);
 int bl = analogRead(bottomleft);
 int br = analogRead(bottomright); 
//print resistance values to serial monitor for debugging
  Serial.println(tr);
  Serial.println(tl);
  Serial.println(bl);
  Serial.println(br);
  Serial.println();
//incremental count increase, continues to show LDR results
  count++;

  int avt= (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  int avgTot = (tr+tl+bl+br)/4;

  int dvert = avt - avd; // check the difference of top and bottom LDRs
  int dhoriz = avl - avr;// check the difference of left and right LDRs

if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
  if (avt > avd) //if average resistance on top is greater than on bottom
  {
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   // set speed out of possible range 0~255
   analogWrite(enB, 10);
  }
  else //avg resistance on bottom is greater than on top
  {
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // set speed out of possible range 0~255
   analogWrite(enB, 10); 
  }
}
  else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
  {
   digitalWrite(in3, LOW);
   digitalWrite(in4, LOW);
  }

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
  if (avl > avr)
  {
   digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   // set speed to 200 out of possible range 0~255
   analogWrite(enA, 200); 
  }
  else
  {
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   // set speed to 200 out of possible range 0~255
   analogWrite(enA, 200); 
  }
}
  else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
  {
   digitalWrite(in1, LOW);
   digitalWrite(in2, LOW);
  }
  delay(dtime);
}

code that is not working…using abs() function/macro here

// connect motor controller pins to Arduino digital pins
// pins on arduino uno for pwm (pulse width modulation) are 3,5,6,9,10,11
// motor one, azimuth adjustment
int enA = 7;
int in1 = 5;
int in2 = 6;
// motor two, elevation adjustment
int enB = 8;
int in3 = 9;
int in4 = 10;

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;
int topright = 0; //LDR top right
int topleft = 1; //LDR top left 
int bottomleft = 2; //LDR bottom left
int bottomright = 3; //LDR bottom right 

//delay time in milliseconds
int dtime = 20; 
//range of tolerance between LDR readings
int tolerance = 100;
//start millisecond count of LDR readings
int count = 0; 

void setup() 
{
 // initialize the serial port:
 Serial.begin(9600); 
 // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void turnclockwise()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed out of possible range 0~255
  analogWrite(enA, 100); 
}

void turncounterclockwise()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // set speed out of possible range 0~255
  analogWrite(enA, 100); 
}

void horizontalstop()
{
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
}

void turnup()
{
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed out of possible range 0~255
  analogWrite(enB, 10);
}

void turndown()
{
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // set speed out of possible range 0~255
   analogWrite(enB, 10);
}

void verticalstop()
{
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void loop()
{
 int tr = analogRead(topleft);
 int tl = analogRead(topright);
 int bl = analogRead(bottomleft);
 int br = analogRead(bottomright); 
//print resistance values to serial monitor for debugging
  Serial.println(tr);
  Serial.println(tl);
  Serial.println(bl);
  Serial.println(br);
  Serial.println();
//incremental count increase, continues to show LDR results
  count++;

  int avt= (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  int avgTot = (tr+tl+bl+br)/4;

  int dvert = avt - avd; // check the difference of top and bottom LDRs
  int dhoriz = avl - avr;// check the difference of left and right LDRs


if (abs(tolerance) < dvert) // check if the difference is in the tolerance else change vertical angle
{
  if (avt > avd) //if average resistance on top is greater than on bottom
  {
  turnup();
  }
  else //avg resistance on bottom is greater than on top
  {
  turndown(); 
  }
}
  else if (abs(tolerance) > dvert) //if difference is smaller than tolerance, stop vertical stepper
  {
  verticalstop();
  }

if (abs(tolerance) < dhoriz) // check if the difference is in the tolerance else change horizontal angle
{
  if (avl > avr)
  {
  turnclockwise();
  }
  else
  {
  turncounterclockwise();
  }
}
  else if (abs(tolerance) > dhoriz) //if difference in horizontal
  {
  horizontalstop(); 
  }
  delay(dtime);
}

Couldn’t find an instance of abs() in your code, but made an edit that may help with your concept of void loop() :wink:

#define returnNothing void
#define returnSmallUnsignedNumber byte
#define MediumSignedNumber int

// connect motor controller pins to Arduino digital pins
// pins on arduino uno for pwm (pulse width modulation) are 3,5,6,9,10,11
// motor one, azimuth adjustment
int enA = 7;
int in1 = 5;
int in2 = 6;
// motor two, elevation adjustment
int enB = 8;
int in3 = 9;
int in4 = 10;

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;

//LDR top right
int topright = 0;

//LDR top left
int topleft = 1;

//LDR bottom left
int bottomleft = 2;

//LDR bottom right
int bottomright = 3;

//delay time in milliseconds
int dtime = 20;

//range of tolerance between LDR readings
int tolerance = 100;

//start millisecond count of LDR readings
int count = 0;

returnNothing setup()
{
 // initialize the serial port:
 Serial.begin(9600);
 // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}
returnNothing loop()
{
 int tr = analogRead(topleft);
 int tl = analogRead(topright);
 int bl = analogRead(bottomleft);
 int br = analogRead(bottomright);
//print resistance values to serial monitor for debugging
  Serial.println(tr);
  Serial.println(tl);
  Serial.println(bl);
  Serial.println(br);
  Serial.println();
//incremental count increase, continues to show LDR results
  count++;

  int avt= (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  int avgTot = (tr+tl+bl+br)/4;

  int dvert = avt - avd; // check the difference of top and bottom LDRs
  int dhoriz = avl - avr;// check the difference of left and right LDRs


if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
  if (avt > avd) //if average resistance on top is greater than on bottom
  {
  turnup();
  }
  else //avg resistance on bottom is greater than on top
  {
  turndown();
  }
}
  else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
  {
  verticalstop();
  }

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
  if (avl > avr)
  {
 returnNothing turnclockwise();
  }
  else
  {
  turncounterclockwise();
  }
}
  else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
  {
  horizontalstop();
  }
  delay(dtime);
}

returnNothing turnclockwise()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed out of possible range 0~255
  analogWrite(enA, 100);
}

returnNothing turncounterclockwise()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // set speed out of possible range 0~255
  analogWrite(enA, 100);
}

returnNothing horizontalstop()
{
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
}

returnNothing turnup()
{
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed out of possible range 0~255
  analogWrite(enB, 10);
}

returnNothing turndown()
{
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // set speed out of possible range 0~255
   analogWrite(enB, 10);
}

returnNothing verticalstop()
{
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

ahh ic thanks...

i edited my last post and included the correct code with regards to the abs() function.

well i thought i would post my code that’s working if anyone wants to use it…
it should work for any sized dc motor as long as the proper power source and h bridge driver are used and of course the connections should be wired properly…

#define motormoves void

// connect motor controller pins to Arduino digital pins
// pins on arduino uno for pwm (pulse width modulation) are 3,5,6,9,10,11
// motor one, azimuth adjustment
int enA = 3;
int in1 = 5;
int in2 = 6;
// motor two, elevation adjustment
int enB = 9;
int in3 = 10;
int in4 = 11;

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;

//LDR top right
int topright = 0;

//LDR top left 
int topleft = 1; 

//LDR bottom left
int bottomleft = 2; 

//LDR bottom right
int bottomright = 3; 

//delay time in milliseconds
int dtime = 20; 

//range of tolerance between LDR readings
int tolerance = 100;

//start millisecond count of LDR readings
int count = 0; 

void setup() 
{
 // initialize the serial port:
 Serial.begin(9600); 
 // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}
void loop()
{
 int tr = analogRead(topleft);
 int tl = analogRead(topright);
 int bl = analogRead(bottomleft);
 int br = analogRead(bottomright); 
//print resistance values to serial monitor for debugging
  Serial.println(tr);
  Serial.println(tl);
  Serial.println(bl);
  Serial.println(br);
  Serial.println();
//incremental count increase, continues to show LDR results
  count++;

  int avt= (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  int avgTot = (tr+tl+bl+br)/4;

  int dvert = avt - avd; // check the difference of top and bottom LDRs
  int dhoriz = avl - avr;// check the difference of left and right LDRs


if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
  if (avt > avd) //if average resistance on top is greater than on bottom
  {
  turnup();  //vertical motor moves up
  }
  else //avg resistance on bottom is greater than on top
  {
  turndown(); //vertical motor moves down
  }
}
  else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
  {
  verticalstop();
  }

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
  if (avl > avr)
  {
  turnclockwise(); //horizontal motor turns clockwise
  }
  else
  {
  turncounterclockwise(); //horizontal motor turns counterclockwise
  }
}
  else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
  {
  horizontalstop(); 
  }
  delay(dtime);
}

motormoves turnclockwise()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // set speed out of possible range 0~255
  analogWrite(enA, 200); 
}

motormoves turncounterclockwise()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed out of possible range 0~255
  analogWrite(enA, 200); 
}

motormoves horizontalstop()
{
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
}

motormoves turnup()
{
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  // set speed out of possible range 0~255
  analogWrite(enB, 200);
}

motormoves turndown()
{
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   // set speed out of possible range 0~255
   analogWrite(enB, 200);
}

motormoves verticalstop()
{
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}