motor control

hi guys im actually working on a solar tracker module and using a continuous servo to do the job, i have a code, it works on the principle that it takes readings from photo resistors and compares where is the highest voltage on left or right and feeds to servo to rotate that direction however with me it rotates but doesn’t stop when the light is equal or at least nearly equal on both ( photo resistors) i hope you can help

#include <Servo.h> 
 
Servo myservo; 
int pos = 90;   // initial position
int sens1 = A0; // LRD 1 pin
int sens2 = A1; //LDR 2 pin
int tolerance = 2;
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode(sens1, INPUT);
  pinMode(sens2, INPUT);
  myservo.write(pos);
  delay(2000); // a 2 seconds delay while we position the solar panel
}  
 
void loop() 
{ 
  int val1 = analogRead(sens1); // read the value of sensor 1
  int val2 = analogRead(sens2); // read the value of sensor 2
 
  if((abs(val1 - val2) <= tolerance) || (abs(val2 - val1) <= tolerance)) {
    //do nothing if the difference between values is within the tolerance limit
  } else {    
    if(val1 > val2)
    {
      pos = --pos;
    }
    if(val1 < val2) 
    {
      pos = ++pos;
    }
  }
 
  if(pos > 180) { pos = 180; } // reset to 180 if it goes higher
  if(pos < 0) { pos = 0; } // reset to 0 if it goes lower
  
  myservo.write(pos); // write the position to servo
  delay(50);
}

servo motor : Arduino T010051 Digital Continuous Servo Module (360°) | Rapid Online

hamzah95:

 if((abs(val1 - val2) <= tolerance) || (abs(val2 - val1) <= tolerance)) {

}

Aren’t these both the same thing? abs(val1-val2) = abs(val2-val1).

It seems when you reach your set point, you would still be giving a command of 0 or 180 since you do nothing when you are within tolerance, the servo would keep rotating, if you write 90 to the servo it should stop there. Its a continuous rotation servo, instead of thinking of position just give it fixed numbers. Any number greater than 90 will go one direction and less than 90 should go the other. myservo.write(90). should make it stop.

A standard servo would be more suited to the way you are intending your code to work.

thank you for your answer sir. how would i write it or where would i place the line "myservo.write(90)." so that it stops when both getting approximately the same light. (im not the greatest at code) that's why i'm asking would appreciate your help sir.