0

I developed an android app to control a robot using nodemcu. I have added some buttons to on/off or blink some LEDs. It works fine but, if I on the one LED or drive the robot, the other LEDs goes off. What I want is the LEDs should be off only if I press the off button for that respective LED.

My code for nodemcu -

#define RIGHT_MOTOR_1  15       // L298N in1 motors Right           GPIO15(D8)
#define RIGHT_MOTOR_2  13       // L298N in2 motors Right           GPIO13(D7)
#define LEFT_MOTOR_1   2        // L298N in3 motors Left            GPIO2(D4)
#define LEFT_MOTOR_2   0        // L298N in4 motors Left            GPIO0(D3)

#define FRONT_LED    16        //Front LED        GPIO14(D0)
#define REAR_LED_1   5         //Rear LED        GPIO12(D1)
#define REAR_LED_2   4         //Rear LED        GPIO12(D2)


#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

String command;

const char* ssid = "NodeMCU";
const char* password = "10101010";

ESP8266WebServer server(80);

void setup() {

  pinMode(RIGHT_MOTOR_1, OUTPUT); 
  pinMode(RIGHT_MOTOR_2, OUTPUT); 
  pinMode(LEFT_MOTOR_1, OUTPUT); 
  pinMode(LEFT_MOTOR_2, OUTPUT);  
  pinMode(FRONT_LED, OUTPUT); 
  pinMode(REAR_LED_1, OUTPUT); 
  pinMode(REAR_LED_2, OUTPUT); 

  Serial.begin(74880);

  // Connecting WiFi
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);

  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();    
}

//LED Logic
void frontLEDon(){
  digitalWrite(FRONT_LED, HIGH);
}

void frontLEDoff(){
  digitalWrite(FRONT_LED, LOW);
}

void frontLEDblink(){
    digitalWrite(FRONT_LED, HIGH);
    delay(500); 
    digitalWrite(FRONT_LED, LOW); 
    delay(500); 
}

void rearLEDon(){
  digitalWrite(REAR_LED_1, HIGH);
  digitalWrite(REAR_LED_2, HIGH);
}

void rearLEDoff(){
  digitalWrite(REAR_LED_1, LOW);
  digitalWrite(REAR_LED_2, LOW);
}

void rearLEDblink(){
    digitalWrite(REAR_LED_1, HIGH);
    delay(50); 
    digitalWrite(REAR_LED_1, LOW); 
    delay(50); 
    digitalWrite(REAR_LED_1, HIGH);
    delay(50); 
    digitalWrite(REAR_LED_1, LOW); 
    delay(50); 
    digitalWrite(REAR_LED_2, HIGH);
    delay(50); 
    digitalWrite(REAR_LED_2, LOW); 
    delay(50); 
    digitalWrite(REAR_LED_2, HIGH);
    delay(50); 
    digitalWrite(REAR_LED_2, LOW); 
    delay(50); 
}

void forward(){ 
  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, HIGH);
  digitalWrite(LEFT_MOTOR_1, LOW);
  digitalWrite(LEFT_MOTOR_2, HIGH);
}

void backward(){ 
  digitalWrite(RIGHT_MOTOR_1, HIGH);
  digitalWrite(RIGHT_MOTOR_2, LOW);
  digitalWrite(LEFT_MOTOR_1, HIGH);
  digitalWrite(LEFT_MOTOR_2, LOW);
}

void right(){ 
  digitalWrite(RIGHT_MOTOR_1, HIGH);
  digitalWrite(RIGHT_MOTOR_2, LOW);
  digitalWrite(LEFT_MOTOR_1, LOW);
  digitalWrite(LEFT_MOTOR_2, HIGH);
}

void left(){ 
  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, HIGH);
  digitalWrite(LEFT_MOTOR_1, HIGH);
  digitalWrite(LEFT_MOTOR_2, LOW);
}

void robotStop(){ 
  digitalWrite(RIGHT_MOTOR_1, LOW);
  digitalWrite(RIGHT_MOTOR_2, LOW);
  digitalWrite(LEFT_MOTOR_1, LOW);
  digitalWrite(LEFT_MOTOR_2, LOW);
}

void loop(){
      server.handleClient();
      command = server.arg("State");


      if(command == "Forward"){
        forward();
        frontLEDon();
      }
      if(command == "Backward"){
        backward();
        rearLEDon();
      }
      if(command == "Left"){
        left();
      }
      if(command == "Right"){
        right();
      }
      if(command == "Stop"){
        robotStop();
      }
      if(command == "FrontLEDon"){
        frontLEDon();
      }
      if(command == "FrontLEDblink"){
        frontLEDblink();
      }
      if(command == "FrontLEDoff"){
        frontLEDoff();
      }
      if(command == "RearLEDon"){
        rearLEDon();
      }
      if(command == "RearLEDblink"){
        rearLEDblink();
      }
      if(command == "RearLEDoff"){
        rearLEDoff();
      }
}

void HTTP_handleRoot(void) {
  if( server.hasArg("State") ){
       Serial.println(server.arg("State"));
  }
  server.send ( 200, "text/html", "" );
  delay(1);
}
  • the android app may be the cause – jsotola May 28 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.