#include <Servo.h>
const int botonPin = 2; // Pin del botón
const int servoPin = 9; // Pin del servomotor
Servo servo; // Objeto Servo para controlar el servomotor
int botonEstadoAnterior = HIGH; // Estado anterior del botón
int anguloActual = 180; // Ángulo actual del servomotor
void setup() {
pinMode(botonPin, INPUT_PULLUP); // Configura el pin del botón como entrada con resistencia de pull-up
servo.attach(servoPin); // Adjunta el servomotor al pin
servo.write(anguloActual); // Inicialmente, mueve el servomotor a la posición 180°
}
void loop() {
int botonEstado = digitalRead(botonPin); // Lee el estado del botón
// Si el estado del botón cambia de alto a bajo (se presiona el botón)
if (botonEstado == LOW && botonEstadoAnterior == HIGH) {
// Cambia el ángulo del servomotor
if (anguloActual == 180) {
servo.write(90); // Mueve el servomotor a la posición 90°
anguloActual = 90;
} else {
servo.write(180); // Mueve el servomotor a la posición 180°
anguloActual = 180;
}
}
botonEstadoAnterior = botonEstado; // Actualiza el estado anterior del botón
}