#include <ESP32Servo.h>
Servo miServo;
#define pinServo 32
#define BUTTON_A 23 // Pin del botón central
#define BUTTON_L 22 // Pin del botón izquierdo
#define BUTTON_R 19 // Pin del botón derecho
#define BUTTON_C 21 // Pin del botón central
#define Motor_PIN 25 // Pin del LED o motor
int llanta = 90; // Inicializamos la posición del servo en 90° (central)
void setup() {
pinMode(BUTTON_A, INPUT); // Configura el botón central como entrada
pinMode(BUTTON_L, INPUT); // Configura el botón izquierdo como entrada
pinMode(BUTTON_R, INPUT); // Configura el botón derecho como entrada
pinMode(BUTTON_C, INPUT); // Configura el botón central como entrada
pinMode(Motor_PIN, OUTPUT); // Configura el pin del LED o motor como salida
digitalWrite(Motor_PIN, LOW); // Asegúrate de que el LED o motor inicie apagado
miServo.attach(pinServo, 500, 2500); // Conectar el servo al pin definido
miServo.write(llanta); // Colocar el servo en la posición inicial
}
void loop() {
// Botón izquierdo (BUTTON_R) incrementa la posición del servo
if (digitalRead(BUTTON_R) == LOW) {
llanta = llanta + 15;
if (llanta > 180) llanta = 180; // Asegurarse de que no se pase de 180°
delay(200); // Evitar que el botón sea demasiado sensible (debouncing)
}
// Botón central (BUTTON_C) mueve el servo a 90° (posición central)
if (digitalRead(BUTTON_C) == LOW) {
llanta = 90; // Establece el servo en la posición central
delay(200);
}
// Botón derecho (BUTTON_L) decrementa la posición del servo
if (digitalRead(BUTTON_L) == LOW) {
llanta = llanta - 15;
if (llanta < 0) llanta = 0; // Asegurarse de que no se pase de 0°
delay(200);
}
miServo.write(llanta); // Mueve el servo a la posición actual de 'llanta'
// Botón central (BUTTON_A) enciende o apaga el LED o motor
if (digitalRead(BUTTON_A) == LOW) {
digitalWrite(Motor_PIN, HIGH); // Enciende el LED o motor cuando el botón central está presionado
} else {
digitalWrite(Motor_PIN, LOW); // Apaga el LED o motor cuando el botón no está presionado
}
}