//Fuente Chip-Scope https://github.com/Dlloydev/Wokwi-Chip-Scope
// tutorial servo https://blog.wokwi.com/learn-servo-motor-using-wokwi-logic-analyzer/
const int servoPin = 16;
// PWM configuración
const int freq = 50; //frecuencia = 50hz => periodo 20ms
const int resolution = 16;
//Formula calculo Duty
//Duty = (pulso / 20ms) * 65535
const int dutyMin = 1638; // 0.5 ms → 0°
const int dutyMax = 8191; // 2.5 ms → 180°
int duty = dutyMin;
int incremento = 20; // velocidad más suave
// Temporización
unsigned long tiempoAnterior = 0;
const unsigned long intervalo = 20; // ms
void setup() {
ledcAttach(servoPin, freq, resolution);
}
void loop() {
unsigned long tiempoActual = millis();
if (tiempoActual - tiempoAnterior >= intervalo) {
tiempoAnterior = tiempoActual;
duty += incremento;
// Cambiar dirección
if (duty >= dutyMax || duty <= dutyMin) {
incremento = -incremento;
}
ledcWrite(servoPin, duty);
}
}