Hello, I'm working with an ESP32 to create a scale model, but have truble making a Servo work.
With a siple code the servo works properly (like tutorialas on "how to use a servo" "move fron 0 to 170"), but I need it to be part of a bigger one which controls other elements.
With the ESP32 I create a web server with sliders and this one in particular works fine (it sends the correct data) activates a led but doesn't controls the servo position.
The slider is the "time of the day" and it is recived fine. Later that hour is converted by map funcion to the position of the servo, thats where the problem is cause the position is writen but not moves the servo. Truble in the "Solar" section.
I check that my power sourse works (5V 20A), which the led is also being powered.
The code is just what I think is not working
#include <WiFi.h>
#include <ESP32Servo.h>
const char* ssid = "INVITADOS";
const char* password = "invitados00--";
const int Sled1 = 17;
Servo myServo;
const int servoPin = 16;
WiFiServer server(80);
String sliderValue2 = "0";
<!DOCTYPE html>
<html>
<head>
<title>Simulador Energías Renovables</title>
<body>
<h3>Solar
<a href="https://drive.google.com/file/d/1hKnpoevKSGnvRUTPZ1p7uJbzRIjdUV9z/view?usp=sharing" target="_blank" tittle"Explicación sistema Solar">ℹ</a>
</h3>
<input type="range" min="0" max="24" value="0" class="slider" id="slider2" oninput="updateSlider2(this.value)">
<p>Hora del dia: <span id="value2">0</span></p>
<script>
function updateSlider2(val) {
document.getElementById('value2').innerText = val;
fetch(`/slider2?value=${val}`);
}
</script>
</body>
</html>
)rawliteral";
void setup() {
Serial.begin(115200);
//Solar
pinMode(Sled1, OUTPUT);
analogWrite(Sled1, 0);
myServo.attach(servoPin, 700, 2300);
Serial.print("Conectando a: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConexión WiFi exitosa <3");
Serial.print("Dirección IP: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
unsigned long tiempoInicio = millis ();
String request = client.readStringUntil('\r');
client.flush();
if (request.indexOf("GET / ") >= 0) {
handleRoot(client);
} else if (request.indexOf("GET /slider1?") >= 0) {
handleSlider(client, request, sliderValue1);
int vel = sliderValue1.toInt();
int pwmE = map(vel, -1, 20, 0, 255);
analogWrite(EOS1, pwmE);
if (vel < 3){
analogWrite (EOS1, 0);
analogWrite (Em2, 0);
analogWrite (Eled1, 0);
analogWrite (Eled2, 0);
EOS = 0;
} else if (vel < 22){
analogWrite (EOS1, pwmE);
analogWrite (Em2, pwmE);
EOS = 1;
eolicoParpadeo = true;
TiempoEolico = millis ();
analogWrite (Eled1, 255);
analogWrite (Eled2, 255);
} else {
analogWrite (EOS1, 0);
analogWrite (Em2, 0);
analogWrite (Eled1, 0);
analogWrite (Eled2, 0);
EOS = 0;
}
//unsigned long tiempoFinal = millis();
Serial.println("Velocidad del viento: " + sliderValue1);
//Serial.print("Tiempo: ");
//Serial.println(tiempoFinal-tiempoInicio);
//Solar
} else if (request.indexOf("GET /slider2?") >= 0) {
handleSlider(client, request, sliderValue2);
int hora = sliderValue2.toInt();
int pos = map(hora, 8, 18, 0, 180);
myServo.write(pos);
delay(20);
if (hora >= 8 && hora <= 13){
int ledS = map(hora, 8, 13, 20, 255);
analogWrite(Sled1, ledS);
SOL = 1;
} else if (hora >= 13 && hora <= 18){
int ledS = map(hora, 13, 18, 255, 20);
analogWrite(Sled1, ledS);
SOL = 1;
} else {
analogWrite(Sled1, 0);
SOL = 0;
}
//unsigned long tiempoFinal = millis();
Serial.println("Hora del día: " + sliderValue2 + " Posición: " + pos);
Serial.println(map(pos, 0, 180, 500, 2500));
}
}
}