servo motor speed

hi guys,

i’m using a servo motor to move clockwise or anti clock wise, using some LDRs and an arduino the main purpose to track the sun im attaching the servo to the outer rim of a turntable with a very smooth bearing so that it tracks the light and moves the table, i found a code online and used it, it works however the motor is slow in reaction and in movement, as i need it to be faster, im not the best at code so i thought you can help, i really need some help, the code is bellow:

#include <Servo.h> 

Servo myservo; 
unsigned long time;
unsigned long timetest;
int val = 0;
int down = 255;
int up = 0;
int pos = 90;

int InL = 0;
int InC = 1;
int InR = 2;

int BaseL = 0;
int BaseC = 0;
int BaseR = 0;

int OutL = 0;
int OutC = 0;
int OutR = 0;

int difL = 0;
int difR = 0;


void setup() 
{ 
 Serial.begin(9600); 
 pinMode(InL, INPUT);
 pinMode(InC, INPUT);
 pinMode(InR, INPUT);
 
 BaseL = analogRead(InL);
 BaseC = analogRead(InC);
 BaseR = analogRead(InR);

 myservo.attach(9); 
 myservo.write(pos);
 
 //time = millis();   
} 

void loop()
{

 OutL = analogRead(InL) - BaseL+511;
 Serial.println(OutL);
 OutC = analogRead(InC) - BaseC+511;
 Serial.println(OutC);
 OutR = analogRead(InR) - BaseR+511;
 Serial.println(OutR);
 
difL = OutC - OutL;
difR = OutC - OutR;

 if (OutL < OutC && difL > difR)
 {
  moveL(); 
 }
 if (OutR < OutC && difR > difL)
 {
  moveR(); 
 }
 Serial.println(".....");
 delay(25);
 timetest = millis() - time;
 //if (timetest > 20000)
 //{
  // reset();
 //}
}

void moveL()
{
 difL = OutC - OutL;
 if (difL > 4)
 {
   pos = pos + 1 ;                            
   myservo.write(pos); 
   difL= OutC - OutL;
   if(difL < 4)
   {
     reset();
   }
   if(pos == 0)
   {
     for(pos = 0; pos < 90; pos++)  
     {                                  
       myservo.write(pos);              
       delay(25);                       
     } 
   }      
   Serial.println("Moving Left");
   Serial.println("New position: " + (pos));
   delay(55);
 }                      
 return;
} 
 
void moveR()
{
 difR = OutC - OutR;
 if (difR > 4)
 {
   pos = pos - 1 ;                             
   myservo.write(pos);             
   difR = OutC - OutR;
   if(difR < 4)
   {
     reset();
   } 
   if(pos == 0)
   {
     for(pos = 180; pos > 90; pos--)  
     {                                  
       myservo.write(pos);              
       delay(25);                       
     } 
   }
   Serial.println("Moving Right");
   Serial.println("New position: ");
   Serial.println(pos);
   delay(5);
 }                      
 return;
} 
 
 
void reset()
{
  BaseL = analogRead(InL);
  BaseC = analogRead(InC);
  BaseR = analogRead(InR);
  time = millis();
}

servo link: https://www.kitronik.co.uk/2542-futaba-s3003-servo.html

  1. Please use [ code ] tags when posting code. The forum software eats some of the code if you don’t.

  2. Which servo? Manufacturer and model number please. Or a link to the actual page on the website where you bought it, ebay listing page or whatever.

  3. How are you powering it? The usual cause for slow servos is insufficient power. Note the Arduino 5V pin can’t deliver enough power for a servo.

  4. Is it really a servo? Most radio-control type servos will use more power than the solar cell they’re holding can produce.

hamzah95:
however the motor is slow in reaction and in movement,

How could a servo be slow in comparison to the sun?

Are you using a continuous rotation servo?

And please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

…R

You have a magic number in there (4) for the hysteresis, how about #defining it and reducing it
to see if that helps.

You have random looking delays - they will slow things down, lose them (except from the sweeps)

Move the sweep code (triggered when pos == 0) into separate functions to reduce clutter.

The hysteresis tests can be moved into loop and lots of clutter removed from the moveL and moveR
functions (OutL/C/R and difL/R don’t need to be global and don’t need to be referenced in moveL/moveR,
just make moveL and moveR move left and right, keep the decision about when to move in loop - don’t
mix stuff up, its going to cause you confusion in the future.

Something like:

  int difL = OutC - OutL;
  int difR = OutC - OutR;

  if (difL > HYSTERESIS && difL > difR)
    moveL();
  else if (difR > HYSTERESIS && difR > difL)
    moveR();

in loop(), and

void moveR()
{
 pos-- ;
 myservo.write(pos);            
 if(pos == 0)
   sweepR () ;
 Serial.println("Moving Right");
 Serial.println("New position: ");
 Serial.println(pos);
}

void sweepR ()
{
 for(pos = 0; pos < 90; pos++)  
 { 
   myservo.write(pos);              
   delay(25);                      
 }
}

and ditto for left.