#include <Servo.h>
#define SERVO_PIN 9
#define BUTTON_PIN_RIGHT 2
#define BUTTON_PIN_LEFT 3
Servo myServo;
int initialPosition = 90; // Posición inicial del servomotor (90 grados)
int targetPositionRight = 130; // Posición objetivo después de girar 46 grados hacia la derecha (90 + 46)
int targetPositionLeft = 50; // Posición objetivo después de girar 46 grados hacia la izquierda (90 - 46)
int buttonStateRight = 0;
int buttonStateLeft = 0;
bool servoMoving = false;
void setup() {
myServo.attach(SERVO_PIN);
pinMode(BUTTON_PIN_RIGHT, INPUT_PULLUP); // Habilitar resistencia pull-up interna para el pulsador derecho
pinMode(BUTTON_PIN_LEFT, INPUT_PULLUP); // Habilitar resistencia pull-up interna para el pulsador izquierdo
myServo.write(initialPosition); // Mover el servomotor a la posición inicial
}
void loop() {
buttonStateRight = digitalRead(BUTTON_PIN_RIGHT);
buttonStateLeft = digitalRead(BUTTON_PIN_LEFT);
if (buttonStateRight == LOW && !servoMoving) {
servoMoving = true;
myServo.write(targetPositionRight); // Mover el servomotor a la posición objetivo hacia la derecha
// Esperar 3 segundos antes de volver a la posición inicial
delay(150);
myServo.write(initialPosition); // Mover el servomotor a la posición inicial
servoMoving = false;
}
if (buttonStateLeft == LOW && !servoMoving) {
servoMoving = true;
myServo.write(targetPositionLeft); // Mover el servomotor a la posición objetivo hacia la izquierda
// Esperar 3 segundos antes de volver a la posición inicial
delay(150);
myServo.write(initialPosition); // Mover el servomotor a la posición inicial
servoMoving = false;
}
}