NodeMcu Blynk Car

Hello,
I Have Made A Accelerometer And Joystick Controlled Internet-Based NodeMcu Car With Blynk App. I Have Used Accelerometer Of The Phone With Gives The Value Between -9 To 9 On X And Y Axis And Blynk Joystick With Values- -1, 0 And 1 On Each Axis(To Simplify The Code). The Is A Button To Switch Between Accelerometer And Joystick Modes, A Realtime Speed Control Slider And 4 LEDs To Know Which Command I'm Sending Through My Phone Accelerometer(As It Is Very Hard To Configure Between The Coarse Values).

I Have Inversed The Axis In Forwarding And Turning Commands Because I Held The Phone In Landscape Mode During Accelerometer And In Portrait While Using Joystick.

The Components I'm Using Are A NodeMcu, 4DC BO Motors, And A L298N Motor Driver To Drive 4 Motors(Left Motors On One-Socket And Right Motors On Other Socket).

Code Is In The Next Post As Character Limit Exceeded.


This Is My Blynk Setup

I've Used Int Before As I Can't Used Blynk Variable In Code And Made New Void And Timers As I Can't Use 'Blynk' Commands In Loop And Also To Simplify The Code.

Now, I Want To Still Shorten The Code And More Simplify It. Please Give Suggestions.

Also, I Want Suggestions For How Many Amps And Volts Should THe Power Source Have To Power NodeMcu, Motor Driver And Motors.

Kalpit

//#define BLYNK_PRINT Serial  //Blynk Printing

#include <ESP8266WiFi.h>  //Libraries
#include <BlynkSimpleEsp8266.h>  //Libraries

#define Enable1        D1  //PWM Pin For MotorA
#define LeftMotorsF    D2  //Input Pin For MotorA
#define LeftMotorsB    D3  //Input Pin For MotorA
#define RightMotorsF   D4  //Input Pin For MotorB
#define RightMotorsB   D5  //Input Pin For MotorB
#define Enable2        D6  //PWM Pin For MotorB

char auth[] = "AuthToken";  //Authentication Key

char ssid[] = "WifiSSID";  //WiFi Name
char pass[] = "WifiPass";  //WiFi Password

int pwmOutput;  //**Initializing Virtual Pin For Slider First So That I Can Use It In Loop**
int X_Axis;  //Initializing Virtual Pin For Accelerometer First So That I Can Use It In Loop
int Y_Axis;  //Initializing Virtual Pin For Accelerometer First So That I Can Use It In Loop
int buttonState;  //Initializing Virtual Pin For Mode Button First So That I Can Use It In Loop
int XAxis;  //Initializing Virtual Pin For Joystick X-Axis First So That I Can Use It In Loop
int YAxis;//Initializing Virtual Pin For Joystick Y-Axis First So That I Can Use It In Loop

BlynkTimer timer;  //Initialize timer

BlynkTimer timerAccelerometer;  //Initialize timer

BlynkTimer timerJoystick;  //Initialize timer

//Accelerometer
BLYNK_WRITE(V0) {
  
  X_Axis = param[0].asFloat();  //acceleration force applied to X-Axis
  Y_Axis = param[1].asFloat();  //acceleration force applied to Y-Axis

  //Serial.print("X-Axis = ");  //Get Value On Serial Moniter
  //Serial.print(X_Axis);  //Get Value On Serial Moniter
  //Serial.print("  Y-Axis = ");  //Get Value On Serial Moniter
  //Serial.print(Y_Axis);  //Get Value On Serial Moniter
  //Serial.println("  ");  //Get Value On Serial Moniter
  
}

//Slider
BLYNK_WRITE(V1) {
  
  pwmOutput = param.asInt();  // assigning incoming value from slider to a variable
  Serial.println(pwmOutput);
  
}

//Mode Button
BLYNK_WRITE(V7) {
  
  buttonState = param.asInt();  // assigning incoming value from Mode Button to a variable
  
}

BLYNK_WRITE(V8) {
  XAxis = param[0].asInt();
  YAxis = param[1].asInt();
}

WidgetLED LeftLed(V2);  //Initialize LeftLed
WidgetLED StopLed(V3);  //Initialize StopLed
WidgetLED RightLed(V4);  //Initialize RightLed
WidgetLED ForwardLed(V5);  //Initialize ForwardLed
WidgetLED BackwardLed(V6);  //Initialize BackwardLed

void AccelerometerLed()  //Making a New void as we can run Blynk In Loop
{
  if (X_Axis == 7 && Y_Axis == 0 || X_Axis == 0 && Y_Axis == 0)  //Turn StopLed On  
  {
    StopLed.on();
  }
  else  //Turn LeftLed Off
  {
    StopLed.off();  
  }

  if (X_Axis < 7 && X_Axis > 3 && Y_Axis < 3 && Y_Axis > -2)  //Turn ForwardLed On
  {
    ForwardLed.on();
  }
  else  //Turn ForwardLed Off
  {
    ForwardLed.off();  
  }

  if (X_Axis < 11 && X_Axis > 7 && Y_Axis < 3 && Y_Axis > -2)  //Turn BackwardLed On
  {
    BackwardLed.on();
  }
  else  //Turn BackwardLed Off
  {
    BackwardLed.off();
  }

  if (X_Axis < 9 && X_Axis > 4 && Y_Axis < 5 && Y_Axis > 0)  //Turn RightLed On
  {
    RightLed.on();
  }
  else  //Turn RightLed Off
  {
    RightLed.off();
  }

  if (X_Axis < 9 && X_Axis > 4 && Y_Axis < 0 && Y_Axis > -5)  //Turn LeftLed On
  {
    LeftLed.on();
  }
  else  //Turn LefttLed Off
  {
    LeftLed.off();
  }

}

void Accelerometer()  //Making a New void as we can run Blynk In Loop
{
  if(X_Axis < 7 && X_Axis > 3 && Y_Axis < 3 && Y_Axis > -2)
  //FORWARD
    {
      digitalWrite(LeftMotorsF,HIGH);
      digitalWrite(LeftMotorsB,LOW);
      digitalWrite(RightMotorsF,HIGH);
      digitalWrite(RightMotorsB,LOW);
      //Serial.println("Forward");  //Get Value On Serial Moniter
      
    }
    else if(X_Axis < 11 && X_Axis > 7 && Y_Axis < 3 && Y_Axis > -2)
    //BACKWARD
    {
      digitalWrite(LeftMotorsF,LOW);
      digitalWrite(LeftMotorsB,HIGH);
      digitalWrite(RightMotorsF,LOW);
      digitalWrite(RightMotorsB,HIGH);
      //Serial.println("Backward");  //Get Value On Serial Moniter
    }
    else if(X_Axis < 9 && X_Axis > 4 && Y_Axis < 5 && Y_Axis > 0)
    //RIGHT
    {
      digitalWrite(LeftMotorsF,HIGH);
      digitalWrite(LeftMotorsB,LOW);
      digitalWrite(RightMotorsF,LOW);
      digitalWrite(RightMotorsB,HIGH);
      //Serial.println("Right");  //Get Value On Serial Moniter
    }
    else if(X_Axis < 9 && X_Axis > 4 && Y_Axis < 0 && Y_Axis > -5)
    //LEFT
    {
      digitalWrite(LeftMotorsF,LOW);
      digitalWrite(LeftMotorsB,HIGH);
      digitalWrite(RightMotorsF,HIGH);
      digitalWrite(RightMotorsB,LOW);
      //Serial.println("Left");  //Get Value On Serial Moniter
    }
    else if(X_Axis == 7 && Y_Axis == 0 || X_Axis == 0 && Y_Axis == 0)
    //STOP
    {
      digitalWrite(LeftMotorsF,LOW);
      digitalWrite(LeftMotorsB,LOW);
      digitalWrite(RightMotorsF,LOW);
      digitalWrite(RightMotorsB,LOW);
      //Serial.println("Stop");  //Get Value On Serial Moniter
    }
  analogWrite(Enable1, pwmOutput); // Send PWM signal(With The Value Of Slider) to L298N EnablePin1
  analogWrite(Enable2, pwmOutput); // Send PWM signal(With The Value Of Slider) to L298N EnablePin2
 }

void Joystick()  //Making a New void as we can run Blynk In Loop
{
  if(XAxis == 0 && YAxis == 1)
  //FORWARD
    {
      digitalWrite(LeftMotorsF,HIGH);
      digitalWrite(LeftMotorsB,LOW);
      digitalWrite(RightMotorsF,HIGH);
      digitalWrite(RightMotorsB,LOW);
      //Serial.println("Forward");  //Get Value On Serial Moniter
    }
    else if(XAxis == 0 && YAxis == -1)
    //BACKWARD
    {
      digitalWrite(LeftMotorsF,LOW);
      digitalWrite(LeftMotorsB,HIGH);
      digitalWrite(RightMotorsF,LOW);
      digitalWrite(RightMotorsB,HIGH);
      //Serial.println("Backward");  //Get Value On Serial Moniter
    }
    else if(XAxis == 1 && YAxis == 0)
    //RIGHT
    {
      digitalWrite(LeftMotorsF,HIGH);
      digitalWrite(LeftMotorsB,LOW);
      digitalWrite(RightMotorsF,LOW);
      digitalWrite(RightMotorsB,HIGH);
      //Serial.println("Right");  //Get Value On Serial Moniter
    }
    else if(XAxis == -1 && YAxis == 0)
    //LEFT
    {
      digitalWrite(LeftMotorsF,LOW);
      digitalWrite(LeftMotorsB,HIGH);
      digitalWrite(RightMotorsF,HIGH);
      digitalWrite(RightMotorsB,LOW);
      //Serial.println("Left");  //Get Value On Serial Moniter
    }
    else if(XAxis == 0 && YAxis == 0)
    //STOP
    {
      digitalWrite(LeftMotorsF,LOW);
      digitalWrite(LeftMotorsB,LOW);
      digitalWrite(RightMotorsF,LOW);
      digitalWrite(RightMotorsB,LOW);
      //Serial.println("Stop");  //Get Value On Serial Moniter
    }  
  analogWrite(Enable1, pwmOutput); // Send PWM signal(With The Value Of Slider) to L298N EnablePin1
  analogWrite(Enable2, pwmOutput); // Send PWM signal(With The Value Of Slider) to L298N EnablePin2
  }

void setup(){
  
  pinMode(Enable1,OUTPUT);  //Establize Enable1 as Output
  pinMode(LeftMotorsF,OUTPUT);  //Establize LeftMotorsF as Output
  pinMode(LeftMotorsB,OUTPUT);  //Establize LeftMotorsB as Output
  pinMode(RightMotorsF,OUTPUT);  //Establize RightMotorsF as Output
  pinMode(RightMotorsB,OUTPUT);  //Establize RightMotorsB as Output
  pinMode(Enable2,OUTPUT);  //Establize Enable2 as Output
  
  Serial.begin(9600);  //Start Serial Moniter at a baud rate of 115200

  Blynk.begin(auth, ssid, pass);  //Login To Blynk Server With Authentication Key, Wifi Name And Wifi PassWord

  timer.setInterval(100, AccelerometerLed);  //The Void 'AccelerometerLed' Will Be Run Every 100 milliseconds infinitely

  timerAccelerometer.setInterval(100, Accelerometer);  //The Void 'Needed' Will Be Run Every 100 milliseconds infinitely

  timerJoystick.setInterval(100, Joystick);  //The Void 'Needed' Will Be Run Every 100 milliseconds infinitely

}

void loop(){
  Blynk.run(); // Run Blynk

  if (buttonState == 0)
  {
   timer.run();  //Run Timer For void 'AccelerometerLed'
   timerAccelerometer.run();  //Run Timer For void 'Accelerometer'
  }

  else if (buttonState == 1)
  {
    timerJoystick.run();  //Run Timer For void 'Joystick'
  }
    
 }

This Is My Code.