Hello,
I am trying to build a chopper and used a code from the internet for some car using nodeMCU wifi to control it, I'm trying to make some sort of strobe LEDs (just for looks) but everytime I include the LEDs functions they interfere with servo commands and my BLDCs stop working, I tried to put them in a seperate void function and mention it in void loop but still my BLDCs stop working
I want a way to control LEDs and BLDCs separated from each other (and an electronic stability controller later)
Here is my code
#define ENA 14 // Enable/speed motors Right GPIO14(D5)
#define ENB 12 // Enable/speed motors Left GPIO12(D6)
#define IN_1 15 // L298N in1 motors Rightx GPIO15(D8)
#define IN_2 13 // L298N in2 motors Right GPIO13(D7)
#define IN_3 2 // L298N in3 motors Left GPIO2(D4)
#define IN_4 0 // L298N in4 motors Left GPIO0(D3)
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
Servo ESC;
Servo ESC1;
String command; //String to store app command state.
int speedCar = 800; // 400 - 1023.
int speed_Coeff = 3;
const char* ssid = "CH-47";
ESP8266WebServer server(80);
void setup() {
ESC.attach(D2,1018,2000);
ESC1.attach(D8,1018,2000);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
Serial.begin(115200);
// Connecting WiFi
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
// Starting WEB-server
server.on ( "/", HTTP_handleRoot );
server.onNotFound ( HTTP_handleRoot );
server.begin();
}
void strobe(){
digitalWrite (D5,HIGH);
delay (100);
digitalWrite (D5, LOW);
delay (200);
digitalWrite (D6,HIGH);
delay (100);
digitalWrite (D6, LOW);
delay (1800);
}
void goAhead(){
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goBack(){
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goRight(){
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goLeft(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goAheadRight(){
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar/speed_Coeff);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goAheadLeft(){
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar/speed_Coeff);
}
void goBackRight(){
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar/speed_Coeff);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goBackLeft(){
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar/speed_Coeff);
}
void stopRobot(){
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void loop() {
server.handleClient();
command = server.arg("State");
strobe();
if (command == "F") goAhead();
else if (command == "B") goBack();
else if (command == "L") goLeft();
else if (command == "R") goRight();
else if (command == "I") goAheadRight();
else if (command == "G") goAheadLeft();
else if (command == "J") goBackRight();
else if (command == "H") goBackLeft();
else if (command == "0") ESC.write(0),ESC1.write(0);
else if (command == "1") ESC.write(20),ESC1.write(20);
else if (command == "2") ESC.write(30),ESC1.write(40);
else if (command == "3") ESC.write(40),ESC1.write(55);
else if (command == "4") ESC.write(45),ESC1.write(65);
else if (command == "5") ESC.write(50),ESC1.write(70);
else if (command == "6") ESC.write(55),ESC1.write(75);
else if (command == "7") ESC.write(60),ESC1.write(80);
else if (command == "8") ESC.write(65),ESC1.write(85);
else if (command == "9") ESC.write(70),ESC1.write(90);
else if (command == "10") ESC.write(75),ESC1.write(95);
}
void HTTP_handleRoot(void) {
if( server.hasArg("State") ){
Serial.println(server.arg("State"));
}
server.send ( 200, "text/html", "" );
delay(1);
}