Proyecto para controlar dos servos y funciones de una cámara con ethernet shield

Hola a todos.

Soy un novato en Arduino, estoy aprendiendo a través de un proyecto de control de una cámara con dos servos para hacer la función pan-tilt, zoom y enfoque a través de una web alojada en el propio Arduino. La idea es conectar el arduino a un router wifi y controlarlo todo dentro de una red local.

Estuve atascado porque la encriptación del router creaba interferencias en el control de los servos (desconozco porqué). Leyendo y probando (y copy-pasteando código :cold_sweat:) he conseguido que me funcione todo. Pero no perfectamente. En algún momento los servos se "confunden" y no hacen lo que deberían.

El codigo es este:

/*
  Web Server
 Proyecto de controlador web para camara
 */

#include  <SPI.h>
#include  <Ethernet.h>
#include  <Servo.h>  // Incluimos la libreria servo

//mac, IP
byte mac[] = { 0x00, 0x02, 0x0A, 0x0D, 0x06, 0x01 };
byte ip[] = { 192, 168, 1, 177 }; //IPAddress ip(192,168,1,177);
byte gateway[] = { 192, 168, 1, 1 }; // ip router
byte subnet[] = { 255, 255, 255, 0 }; // mascara subnet 
EthernetServer server(80); // puerto ethernet 

//pines out Trans
int zoom1 = 7;
int zoom2 = 8;
int foco1 = 2;
int foco2 = 3;
Servo servohor;  // creando el objeto servo para control
Servo servover;  // otro

int servoPinh     =  6;    // control pin for servo motor  
int minPulseh     =  600;  // minimum servo position  
int maxPulseh     =  2400; // maximum servo position  
int turnRateh     =  40;  // servo turn rate increment (larger value, faster rate)  
int refreshTimeh  =  100;   // time (ms) between pulses (50Hz)   
int centerServoh;         // center servo position  
int pulseWidthh;          // servo pulse width  
int moveServoh;           // raw user input  
long lastPulseh   = 0;    // recorded time (ms) of the last pulse 

int servoPinv     =  5;    // control pin for servo motor  
int minPulsev     =  1400;  // minimum servo position  
int maxPulsev     =  2000; // maximum servo position  
int turnRatev     =  40;  // servo turn rate increment (larger value, faster rate)  
int refreshTimev  =  100;   // time (ms) between pulses (50Hz)  
int centerServov;         // center servo position  
int pulseWidthv;          // servo pulse width  
int moveServov;           // raw user input  
long lastPulsev   = 0;    // recorded time (ms) of the last pulse  

boolean reading = false;
String readString;

void setup()
{
  
Serial.begin(9600);
//iniciar conexion
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();

  pinMode(2, OUTPUT); //pin selected to control
  pinMode(3, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control
  pinMode(5, OUTPUT);  // Set servo pin as an output pin  
  centerServoh = maxPulseh - ((maxPulseh - minPulseh)/2);  
  pulseWidthh = centerServoh;   // Give the servo a starting point (or it floats)
  pinMode(6, OUTPUT);  // Set servo pin as an output pin  
  centerServov = maxPulsev - ((maxPulsev - minPulsev)/2);  
  pulseWidthv = centerServov;   // Give the servo a starting point (or it floats)




}

void loop()
{

EthernetClient client = server.available();
if (client) 
{
  while (client.connected()) 
  {
    if (client.available()) 
    {
      char c = client.read();
      if(readString.length() < 100) 
      {
      readString += c;
      Serial.print(c); 
      }
    if (c == '\n') 
      {
      

     //HTML creacion de formulario
          Serial.println(readString); 

          client.println("HTTP/1.1 200 OK"); 
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Control Camara posicion zoom y foco</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Arduino, Control Camara posicion zoom y foco</H1>");
         
         //Servohor 
          client.println("<a href=\"/?right\"\">DERECHA</a>");
          client.println("<a href=\"/?center\"\">CENTRO</a>"); 
          client.println("<a href=\"/?left\"\">IZQUIERDA</a>"); 
          
          //servover
          client.println("<a href=\"/?up\"\">ARRIBA</a>");
          client.println("<a href=\"/?mid\"\">CENTRO</a>"); 
          client.println("<a href=\"/?down\"\">ABAJO</a>"); 
          
          // zoom y foco
          client.println("<a href=\"/?zoom1\"\">ZOOM+</a>");
          client.println("<a href=\"/?zoom2\"\">ZOOM-</a>");
          client.println("<a href=\"/?foco1\"\">FOCO+</a>");
          client.println("<a href=\"/?foco2\"\">FOCO-</a>");

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(100);
          //stopping client
          client.stop();
   
  
  

          ///////////////////// control arduino pin

     
          if(readString.indexOf("up") >0)  { pulseWidthv = pulseWidthv - turnRatev; } 
          if(readString.indexOf("mid") >0)   { pulseWidthv = centerServov; }
          if(readString.indexOf("down") >0)  { pulseWidthv = pulseWidthv + turnRatev; } 
          
            if (pulseWidthv > maxPulsev) { pulseWidthv = maxPulsev; } 
            if (pulseWidthv < minPulsev) { pulseWidthv = minPulsev; }
        
            if (millis() - lastPulsev >= refreshTimev) 
            {  
            digitalWrite(5, HIGH);   // start the pulse  
            delayMicroseconds(pulseWidthv);  // pulse width  
            digitalWrite(5, LOW);    // stop the pulse  
            lastPulsev = millis(); 
          }         // save the time of the last pulse  
        
         
          if(readString.indexOf("right") >0)  { pulseWidthh = pulseWidthh - turnRateh; } 
          if(readString.indexOf("center") >0)   { pulseWidthh = centerServoh; }
          if(readString.indexOf("left") >0)  { pulseWidthh = pulseWidthh + turnRateh; } 
          
            if (pulseWidthh > maxPulseh) { pulseWidthh = maxPulseh; } 
            if (pulseWidthh < minPulseh) { pulseWidthh = minPulseh; }
        
            if (millis() - lastPulseh >= refreshTimeh) 
            {  
            digitalWrite(6, HIGH);   // start the pulse  
            delayMicroseconds(pulseWidthh);  // pulse width  
            digitalWrite(6, LOW);    // stop the pulse  
            lastPulseh = millis(); 
          }         // save the time of the last pulse  
        
   
         
          if(readString.indexOf("zoom1") >0)
          {
            digitalWrite(7, HIGH);
            delay (300);
            digitalWrite (7, LOW);
                   
            Serial.println("Zoom+");
          }
                    
          if(readString.indexOf("zoom2") >0)
          {
            digitalWrite(8, HIGH);
            delay (300);
            digitalWrite (8, LOW);            
            Serial.println("Zoom-");
          }
          
          if(readString.indexOf("foco1") >0)
          {
            digitalWrite(2, HIGH);
            delay (300);
            digitalWrite (2, LOW);             
            Serial.println("Foco+");
          }
          
          if(readString.indexOf("foco2") >0)
          {
            digitalWrite(3, HIGH);
            delay (300);
            digitalWrite (3, LOW);           
            Serial.println("Foco-");
          }
          
          //clearing string for next read
          readString="";

 
 //break;
          }
      }
   }
}
}     

          
          

.

¿Alguna idea para limpiar o mejorar el código?

Gracias y un saludo.