#include <ESP32Servo.h>
Servo myservo; // crea objeto servo para control del servo
uint8_t ledGreen = 13; // Led verde
uint8_t ledRed = 14; // Led rojo
String data;
int value = 0;
int angServo;
const int PinServo = 6; // Pin en donde se encuentra conectado el Servo
void setup() {
Serial.begin(9600);
// Inicializa el pin del pulsador como entrada con resistencia pull-up interna
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
myservo.attach(PinServo); // attaches the servo on pin 9 to the servo object
myservo.write(0);
}
void loop()
{
/* Etapa de Recepción */
if (Serial.available()) {
data = Serial.readStringUntil('\n');
data.trim(); // Eliminar espacios y saltos de línea
if (data.length() > 0) {
value = data.toInt(); // Convertir a entero
Serial.print("Valor ADC recibido: ");
Serial.println(value);
angServo = map(value, 0, 4095, 0 ,180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(angServo); // posiciona el servo de acuerdo al valor del potenciometro
delay(100);
}
}
} // fin de main