servo's continuous rotation problem

Hi everyone

I am making an solar tracker using Mg995 Srrvo motors problem i am facing is that as soon as i run the program the servo start continuously rotating can any one please help me to figure it out .

i am using arduino uno and external power supply for 2 servos
below is my code :

#include <Servo.h>
//defining Servos
Servo servohori;
int servoh = 0;
int servohLimitHigh = 160;
int servohLimitLow = 20;

Servo servoverti;
int servov = 0;
int servovLimitHigh = 160;
int servovLimitLow = 20;
//Assigning LDRs
int ldrtopl = 2; //top left LDR green
int ldrtopr = 1; //top right LDR yellow
int ldrbotl = 3; // bottom left LDR blue
int ldrbotr = 0; // bottom right LDR orange

void setup ()
{
Serial.begin(9600);
servohori.attach(5);
servohori.write(0);
servoverti.attach(6);
servoverti.write(0);
delay(500);
}

void loop()
{
servoh = servohori.read();
servov = servoverti.read();
//capturing analog values of each LDR
int topl = analogRead(ldrtopl);
int topr = analogRead(ldrtopr);
int botl = analogRead(ldrbotl);
int botr = analogRead(ldrbotr);

// calculating average
int avgtop = (topl + topr) / 2; //average of top LDRs
int avgbot = (botl + botr) / 2; //average of bottom LDRs
int avgleft = (topl + botl) / 2; //average of left LDRs
int avgright = (topr + botr) / 2; //average of right LDRs
Serial.print("avgtop = ");
Serial.print(avgtop);
Serial.print("\n");
Serial.print("avgbot = ");
Serial.print(avgbot);
Serial.print("\n");
Serial.print("avgleft = ");
Serial.print(avgleft);
Serial.print("\n");
Serial.print("avgright = ");
Serial.print(avgright);
Serial.print("\n");
delay(500);

if (avgtop < avgbot)
{
servoverti.write(servov +1);
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
}
delay(10);
}
else if (avgbot < avgtop)
{
servoverti.write(servov -1);
if (servov < servovLimitLow)
{
servov = servovLimitLow;
}
delay(10);
}
else
{
servoverti.write(servov);
}

if (avgleft > avgright)
{
servohori.write(servoh +1);
if (servoh > servohLimitHigh)
{
servoh = servohLimitHigh;
}
delay(10);
}
else if (avgright > avgleft)
{
servohori.write(servoh -1);
if (servoh < servohLimitLow)
{
servoh = servohLimitLow;
}
delay(10);
}
else
{
servohori.write(servoh);
}
delay(50);
}

Thanks

Servos rarely continuously rotate.

Hacked, ex-servos do.

Have you bought the wrong components?

Please remember to use code tags when posting code.

I have use Right components

khalifa_007:
I have use Right components

Seems unlikely, given what you observe. Do both servos do this?

Try deleting all the code in loop and run a test i.e. just run the code in setup. What happens?

i tried to put all my code in void setup but still it is rotating

You may wish to review your choice of components

i am using this servo : https://www.amazon.in/dp/B07QFBJMV5/ref=cm_sw_r_wa_apa_i_8PTmEbCZKRCP0

khalifa_007:
I am making an solar tracker using Mg995 Srrvo motors problem i am facing is that as soon as i run the program the servo start continuously rotating can any one please help me to figure it out .

If your servo is able to rotate continuously then it is not possible to make it move to a position. All you can do is control its speed and direction.

If you need a servo that can be moved to a position then you need to get a regular servo and not a continuous-rotation servo.

...R

Robin2:
If your servo is able to rotate continuously then it is not possible to make it move to a position. All you can do is control its speed and direction.

If you need a servo that can be moved to a position then you need to get a regular servo and not a continuous-rotation not-really-a-servo-anymore.

...R

khalifa_007:
i am using this servo : https://www.amazon.in/dp/B07QFBJMV5/ref=cm_sw_r_wa_apa_i_8PTmEbCZKRCP0

That one clearly says in several places that it is a continuous rotation servo. The MG995 is available in two versions, standard and continuous rotation. You have the wrong one.

Steve

Can anyone suggest me right servo for my application

The MG995 is available in two versions, standard and continuous rotation.

TheMemberFormerlyKnownAsAWOL:

not a continuous-rotation not-really-a-servo-anymore.

I agree with the sentiment but I wish you had written a proper sentence. I try to take some care over what I write.

...R

sentence. Noun.
a set of words that is complete in itself, typically containing a subject and predicate, conveying a statement, question, exclamation, or command, and consisting of a main clause and sometimes one or more subordinate clauses.

The hyphens in "not-really-a-servo-anymore" serve to compound the proper noun "servo" with the adjectival phrase

(I would also argue that there should be a comma after "position" to indicate a pause, and possibly even an Oxford comma after "regular servo")

khalifa_007:
Can anyone suggest me right servo for my application

How about an MG995 that is NOT the continuous rotation 360 degree version? E.g. https://www.amazon.in/MG995-Metal-Servo-Motor-180-degrees/dp/B0896Z4P7B/ref=sr_1_6?

Steve