i made a dual axis solar tracker in which i use 2 servo motors(mg90s by towerpro)
everything is working fine except the motors are jittering
here is my code plz solve it asap
CODE:
#include <Servo.h> // include Servo library
// 180 horizontal MAX
Servo horizontal; // horizontal servo
int servoh = 135; // stand horizontal servo
int servohLimitHigh = 180;
int servohLimitLow=95;
// 65 degrees MAX
Servo vertical; // vertical servo
int servov = 45; // stand vertical servo
int servovLimitHigh = 85;
int servovLimitLow = 15;
// LDR pin connections
int ldrlt = 1; //LDR top left - BOTTOM LEFT
int ldrrt = 0; //LDR top rigt - BOTTOM RIGHT
int ldrld = 3; //LDR down left - TOP LEFT
int ldrrd = 2; //ldr down rigt - TOP RIGHT
void setup()
{
Serial.begin(9600);
// servo connections
// name.attacht(pin);
horizontal.attach(9);
vertical.attach(10);
horizontal.write(135);
vertical.write(45);
delay(3000);
}
void loop()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
//
int dtime = 10;
int tol = 50;
////
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt
Serial.print(avt);
Serial.print(" ");
Serial.print(avd);
Serial.print(" ");
Serial.print(avl);
Serial.print(" ");
Serial.print(avr);
Serial.print(" ");
Serial.print(dtime);
Serial.print(" ");
Serial.print(tol);
Serial.println(" ");
//
//
if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle
{
if (avt > avd)
{
servov = servov-5;
if (servov < servovLimitLow)
{
servov = servovLimitLow;
}
}
else if (avt < avd)
{
servov= servov+5;
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
}
}
vertical.write(servov);
Serial.print("servov======");
Serial.print(servov);
Serial.println(" ");
}
//
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = servoh-5;
if (servoh < servohLimitLow)
{
servoh = servohLimitLow;
}
}
else if (avl < avr)
{
servoh = 5+servoh;
if (servoh > servohLimitHigh)
{
servoh = servohLimitHigh;
}
}
else if (avl = avr)
{
/// // nothing
}
horizontal.write(servoh);
Serial.print("servoh======");
Serial.print(servoh);
Serial.println(" ");
}
delay(100);
}