Hi, I’m working on a project with an ESP8266 and an L298N.
The goal is to control a car using a game controller. On my computer, I read the controller values and send them via WiFi to the ESP8266, which uses the L298N to drive the motors.
The problem is that the motors move backwards but don’t move forward, and this only happens when WiFi is enabled.
When I test the motors individually, they work fine.
The power supply is two 3.7V batteries in series.
Does anyone know what might be causing this or how to fix it?
#include <ESP8266WiFi.h>
const char* ssid = "*****";
const char* password = "******";
const int in1 = D0;
const int in2 = D3;
const int in3 = D4;
const int in4 = D5;
const int ena = D6;
const int enb = D7;
int vel0 = 255;
int vel1 = 255;
WiFiServer server(80); // Servidor en puerto 80
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enb, OUTPUT);
Serial.print("Conectando");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
String data = "";
unsigned long timeout = millis();
while (client.connected() && millis() - timeout < 2000) {
if (client.available()) {
char c = client.read();
if (c == '\n') break; // fin del mensaje
data += c;
}
yield();x
}
//Serial.println("Recibido: " + data);
// Convertir data a int[]
int action[10]; // Ajusta el tamaño máximo esperado
int index = 0;
int start = 0;
for (int i = 0; i <= data.length(); i++) {
if (data[i] == ',' || i == data.length()) {
String val = data.substring(start, i);
action[index] = val.toInt();
index++;
start = i + 1;
}
}
Serial.print(action[0]);
Serial.print(", ");
Serial.print(action[1]);
Serial.print(", ");
Serial.print(action[2]);
Serial.print(", ");
Serial.println(action[3]);
if (action[0] > 0 && action[2] > 0 || action[1] > 0 && action[3] > 0){
detener();
} else {
if (action[0] > 0 ){ avanzaL(); } else { detenerL(); }
if (action[1] > 0 ){ avanzaR(); } else { detenerR(); }
if (action[2] > 0 ){ reversaL(); } else { detenerL(); }
if (action[3] > 0 ){ reversaR(); } else { detenerR(); }
}
}
}
void detener(){
detenerL();
detenerR();
}
void avanzaL(){
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(ena, HIGH);
//Serial.println("AL");
}
void avanzaR(){
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
digitalWrite(enb, HIGH);
//Serial.println("AR");
}
void reversaL(){
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, vel0);
//Serial.println("RL");
}
void reversaR(){
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enb, vel1);
//Serial.println("RR");
}
void detenerL(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(ena, 0);
}
void detenerR(){
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enb, 0);
}