2 Servos con 2 push button en direccion contraria

Hola comunidad, podrian ayudarme con mi codigo???, como podria hacerle para que mis dos servos se muevan en simultaneo, hasta ahora mi codigo hace que al pulsar un boton un servo se mueva 90 grados para luego detenerse y el otro tiene que esperar a que el primero finalize para moverse 90 grados en direccion contraria. Creo que deberia cambiar la funcion for por otra pero no estoy seguro cual. Estoy usando el arduino UNO, con dos servos 180. y dos push button, uno para que se mueve de 90 a 180 y el otro para que regrese a 90

#include <Servo.h>
Servo myservo1;
Servo myservo2;
int posR = 90;
int posL = 90;
int buttonPin1 = 2;  // pushbutton pin
int buttonPin2 = 3;
int servoPin1 = 9;
int servoPin2 = 10;// servo signal pin
void setup() {
  myservo1.attach(servoPin1);
  myservo2.attach(servoPin2);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  myservo1.write(posR);
}
void loop() {
  int buttonState1 = digitalRead(buttonPin1);  
  if (buttonState1 == LOW) {  // if the button is pressed
    for (posR = 90; posR <= 180; posR += 1) {  // move servo from 0 to 180 degrees
      myservo1.write(posR);  // tell servo to go to position
      Serial.print("Current position of servo: ");
      Serial.println(posR);
      delay(150);  // wait 15 milliseconds for the servo to reach the position
    }
    for (posL = 90; posL >= 0; posL -= 1) {  // move servo from 0 to 180 degrees
      myservo2.write(posL);  // tell servo to go to position
      Serial.print("Current position of servo: ");
      Serial.println(posL);
      delay(150);  // wait 15 milliseconds for the servo to reach the position
    }
  }
  int buttonState2 = digitalRead(buttonPin2);  
  if (buttonState2 == LOW) {  // if the button is pressed  
    for (posL = 0; posL <= 90; posL += 1) {  // move servo back from 180 to 0 degrees
      myservo2.write(posL);  // tell servo to go to position
      Serial.print("Current position of servo: ");
      Serial.println(posL);
      delay(150);  // wait 15 milliseconds for the servo to reach the position
  
  }
    for (posR = 180; posR >= 90; posR -= 1) {  // move servo back from 180 to 0 degrees
      myservo1.write(posR);  // tell servo to go to position
      Serial.print("Current position of servo: ");
      Serial.println(posR);
      delay(150);  // wait 15 milliseconds for the servo to reach the position
    
  }
}
}

Gracias por su ayuda!!

Esta línea controla un servo...

Esta línea controla el otro servo...

Coloque ambas líneas de servocontrol una al lado de la otra.
Cambie el nombre de "PosL" y "PosR" a solo "Pos"

Fijate si te sirve de guía, los servos pueden ir a cualquier posición pero llegan a destino al mismo tiempo.

La respuesta esta en el post#2 y no leí la #3 pero debe estar bien.
la única manera que dos servos hagan lo mismo es que esten las órdenes pegadas.
Uno lo hará antes que el otro pero no te darás cuenta y nadie lo haría salvo con instrumental.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.