Problem with Joystick Timing Settings and Arduino Reset

Hello Everyone ...
I am using an arduino to control two motors with joystick and with commands by using computer through arduino srial monitor. I am facing only one problem. When i used joystick and run the motors wirelessly then it works good if I continuously using joystick by giving commands to the motors. When I stopped using joystick to stop the motors and after 10 to 20 seconds again I wanted to run those motors then I would not run although i saw on a serial monitor that commands sent but motors could not run. Again I needed to close the serial monitor and open it again then the motors run and runs good if i continuously giving commands unless I stop and wait for few seconds and then run again.

By giving commands to the motors on a serial monitor then it works very good if I would give commands after 5 minutes no need of reset or restart of arduino.

Actually I wanted to implement timer for 5 seconds if there will be no commands for 5 seconds then the motors would be stopped. I wanted to implement this because If there would be a signal lose during a long distance then it would stop automatically after 5 seconds

Here is the code of the receiving section with motors and Battery using arduino mega 2560

#include <Servo.h>
const int m_1 = 5;
const int m_2 = 6;
const int LED = 13;
String strID = "";

int BaudRate = 4800;
Servo ESC_1; // create servo object to control the ESC 1
Servo ESC_2; // create servo object to control the ESC 2
#define FWD 0
#define BWD 1




//BATTERY READINGS VARIABLES
const int Battery = A0;
float Voltage = 0.0; //input battery voltage
float Voltage1 = 0.0; //output voltage for analog pin A0
float R1 = 1597; // R1 =47k ohms and 27k ohms resistors 7400, 1581, 1597
float R2 = 1000; // R2 =10k ohms resistor 10000, 1000, 1000
int readValue = 0;
long previousMillis_battery = 0;
byte Bat_start_timer = 0;
int battery_wait;
unsigned long currentMillis = 0; // VALID FOR ALL FUNCTIONS(COUNTER)

//BATTERY FUNCTION
void battery()
{
  readValue = analogRead(Battery);
  Voltage1 = readValue * (5.0 / 1023.0);
  Voltage = Voltage1 / (R2 / (R1 + R2));
  if (Bat_start_timer == 0) battery_wait = 2000;
  if (currentMillis - previousMillis_battery > battery_wait) {
    Serial.print("$ABC,"); Serial.print(Voltage); Serial.println("V\n");
    previousMillis_battery = currentMillis;
    battery_wait = 2000; Bat_start_timer = 1;
  }

} // battery FUNCTION END

unsigned int FORWARD = 2000;
unsigned int NEUTRAL = 1500;
unsigned int REVERSE = 1000;

unsigned int APwm = NEUTRAL;
unsigned int BPwm = NEUTRAL;

boolean ADirection = FWD;
boolean BDirection = FWD;
unsigned int AVelocity = 0; // CURRENT VELOCITY OF M1 in 0-100%
unsigned int BVelocity = 0; // CURRENT VELOCITY OF M2 in 0-100%



String inputString = "";
int TaskNumber = 0;

uint32_t msgTimeout = 0ul;
uint32_t timeNow;

unsigned int GetValue(void)// fetch three digit number value from received string and convert to INTEGER
{
  unsigned int Val = 0;
  Val = (inputString[3] - 48) * 100;
  Val = Val + (inputString[4] - 48) * 10;
  Val = Val + (inputString[5] - 48);
  return (Val);
}




void setup()
{
  Serial.begin(BaudRate); //RX0 for radiolink controlling two motors
  ESC_1.attach(m_1);// (pin, min pulse width, max pulse width in microseconds)
  ESC_2.attach(m_2);// (pin, min pulse width, max pulse width in microseconds)
  pinMode(LED, OUTPUT);
  pinMode(Battery, INPUT);
  Serial.println("ABC System Started");
  ESC_1.writeMicroseconds(APwm);
  ESC_2.writeMicroseconds(BPwm);

}



boolean AFwdFlag = 0; //makes sure that the serial only prints once the state
boolean BFwdFlag = 0; //makes sure that the serial only prints once the state



void loop()
{

  currentMillis = millis(); // to monitor voltages on a serial monitor
  battery();  // battery function recall



  timeNow = millis();



  serialEvent();


  if ( TaskNumber == 0 )
  {
    //if we've gone 5-seconds...
    if ( (timeNow - msgTimeout) >= 5000ul )
    {
      //then all stop
      ESC_1.writeMicroseconds(NEUTRAL);
      ESC_2.writeMicroseconds(NEUTRAL);
      digitalWrite(LED, HIGH );  //<---- add this line

    }//if

  }//if

  if ( TaskNumber )
  {
    //each pass where we see a non-zero task #, reset the msgTimeout value
    msgTimeout = millis();
    digitalWrite(LED, LOW );

    if (TaskNumber == 1) Serial.println("OK");
    if (TaskNumber == 2 || TaskNumber == 4) {
      ESC_1.writeMicroseconds(NEUTRAL);
    }
    if (TaskNumber == 3 || TaskNumber == 4) {
      ESC_2.writeMicroseconds(NEUTRAL);
    }


    if (TaskNumber == 5 || TaskNumber == 7) {
      if (ADirection == FWD) {
        // MOTOR A INCREMENT FOR FORWARD DIRECTION
        if (AVelocity > 0 && AVelocity <= 10) {
          APwm = 1535 ;
        }
        else if (AVelocity > 10 && AVelocity <= 20) {
          APwm = 1540;
        }
        else if (AVelocity > 20 && AVelocity <= 30) {
          APwm = 1545;
        }
        else if (AVelocity > 30 && AVelocity <= 40) {
          APwm = 1550;
        }
        else if (AVelocity > 40 && AVelocity <= 50) {
          APwm = 1555;
        }
        else if (AVelocity > 50 && AVelocity <= 60) {
          APwm = 1560;
        }
        else if (AVelocity > 60 && AVelocity <= 70) {
          APwm = 1565;
        }
        else if (AVelocity > 70 && AVelocity <= 80) {
          APwm = 1570;
        }
        else if (AVelocity > 80 && AVelocity <= 90) {
          APwm = 1575;
        }
        else if (AVelocity > 90 && AVelocity <= 100) {
          APwm++ ;
        }
        else {
          APwm = 1500;
        }
        ESC_1.writeMicroseconds(APwm);
        AFwdFlag = 1;
      }

      else {
        if (AFwdFlag == 1)
        {
          ESC_1.writeMicroseconds(NEUTRAL); delay(100);
          ESC_1.writeMicroseconds(REVERSE); delay(100);
          ESC_1.writeMicroseconds(NEUTRAL); delay(100);
          AFwdFlag = 0;
        }
        // MOTOR A DECREMENT FOR BACKWARD DIRECTION
        if (AVelocity > 0 && AVelocity <= 10) {
          APwm = 1430 ;
        }
        else if (AVelocity > 10 && AVelocity <= 20) {
          APwm = 1420;
        }
        else if (AVelocity > 20 && AVelocity <= 30) {
          APwm = 1410;
        }
        else if (AVelocity > 30 && AVelocity <= 40) {
          APwm = 1400;
        }
        else if (AVelocity > 40 && AVelocity <= 50) {
          APwm = 1390;
        }
        else if (AVelocity > 50 && AVelocity <= 60) {
          APwm = 1380;
        }
        else if (AVelocity > 60 && AVelocity <= 70) {
          APwm = 1370;
        }
        else if (AVelocity > 70 && AVelocity <= 80) {
          APwm = 1360;
        }
        else if (AVelocity > 80 && AVelocity <= 90) {
          APwm = 1350;
        }
        else if (AVelocity > 90 && AVelocity <= 100) {
          APwm-- ;
        }
        else {
          BPwm = 1500;
        }
        ESC_1.writeMicroseconds(APwm);


      }//ELSE

    }



    if (TaskNumber == 6 || TaskNumber == 7) {
      if (BDirection == FWD) {
        // MOTOR B INCREMENT FOR FORWARD DIRECTION
        if (BVelocity > 0 && BVelocity <= 10) {
          BPwm = 1535 ;
        }
        else if (BVelocity > 10 && BVelocity <= 20) {
          BPwm = 1540;
        }
        else if (BVelocity > 20 && BVelocity <= 30) {
          BPwm = 1545;
        }
        else if (BVelocity > 30 && BVelocity <= 40) {
          BPwm = 1550;
        }
        else if (BVelocity > 40 && BVelocity <= 50) {
          BPwm = 1555;
        }
        else if (BVelocity > 50 && BVelocity <= 60) {
          BPwm = 1560;
        }
        else if (BVelocity > 60 && BVelocity <= 70) {
          BPwm = 1565;
        }
        else if (BVelocity > 70 && BVelocity <= 80) {
          BPwm = 1570;
        }
        else if (BVelocity > 80 && BVelocity <= 90) {
          BPwm = 1575;
        }
        else if (BVelocity > 90 && BVelocity <= 100) {
          BPwm++ ;
        }
        else {
          BPwm = 1500;
        }

        ESC_2.writeMicroseconds(BPwm);
        BFwdFlag = 1;
      }
      else {
        if (AFwdFlag == 1)
        {
          ESC_2.writeMicroseconds(NEUTRAL); delay(100);
          ESC_2.writeMicroseconds(REVERSE); delay(100);
          ESC_2.writeMicroseconds(NEUTRAL); delay(100);
          BFwdFlag = 0;
        }
        // MOTOR B DECREMENT FOR BACKWARD DIRECTION
        if (BVelocity > 0 && BVelocity <= 10) {
          BPwm = 1430 ;
        }
        else if (BVelocity > 10 && BVelocity <= 20) {
          BPwm = 1420;
        }
        else if (BVelocity > 20 && BVelocity <= 30) {
          BPwm = 1410;
        }
        else if (BVelocity > 30 && BVelocity <= 40) {
          BPwm = 1400;
        }
        else if (BVelocity > 40 && BVelocity <= 50) {
          BPwm = 1390;
        }
        else if (BVelocity > 50 && BVelocity <= 60) {
          BPwm = 1380;
        }
        else if (BVelocity > 60 && BVelocity <= 70) {
          BPwm = 1370;
        }
        else if (BVelocity > 70 && BVelocity <= 80) {
          BPwm = 1360;
        }
        else if (BVelocity > 80 && BVelocity <= 90) {
          BPwm = 1350;
        }
        else if (BVelocity > 90 && BVelocity <= 100) {
          BPwm-- ;
        }
        else {
          BPwm = 1500;
        }
        ESC_2.writeMicroseconds(BPwm);


      }//ELSE
    }


    TaskNumber = 0;
  }
}



void serialEvent() { //radiolink for controlling two motors by command Serial port 0

  while (Serial.available()) {

    char inChar = (char)Serial.read();
    inputString += inChar;

    if (inChar == '\r' || inChar == 10)
    {

      if (inputString[0] == '<')
      {

        if (inputString[1] == 'A' && inputString[2] == 'T' && inputString[3] == '>' ) TaskNumber = 1; // AT

        if (inputString[2] == 'N' && inputString[3] == '>' )
        {
          if ( inputString[1] == 'A') TaskNumber = 2; // Set Motor A to Neutral
          if ( inputString[1] == 'B') TaskNumber = 3; // Set Motor B to Neutral
          if ( inputString[1] == 'C') TaskNumber = 4; // Set Both Motors to Neutral
        }

        if (inputString[6] == '>' )
        {

          if ( inputString[1] == 'A') {
            if (inputString[2] == '+') ADirection = FWD; else ADirection = BWD;
            AVelocity = GetValue();
            TaskNumber = 5;
          }

          if ( inputString[1] == 'B') {
            if (inputString[2] == '+') BDirection = FWD; else BDirection = BWD;
            BVelocity = GetValue();
            TaskNumber = 6;
          }

          if ( inputString[1] == 'C') {
            if (inputString[2] == '+') {
              ADirection = FWD;
              BDirection = FWD;
            }
            else {
              ADirection = BWD;
              BDirection = BWD;
            }
            AVelocity = GetValue();
            BVelocity = GetValue();
            TaskNumber = 7;
          }

        }

      }
      inputString = "";
    }
  }
}

Here is the code of the transmitter with arduino UNO with joystick

//Joystick Latest Code

int VRx = A1; // ANALOG READING FOR LEFT AND RIGHT MOVEMENT FOR X-AXIS
int VRy = A0; // ANALOG READING FOR UPWARD AND DOWNWARD MOVEMENT FOR Y-AXIS
int Sw = 2; //
float Aval; //VALUE OF VOLTAGE AT A1PORT
float Bval; //VALUE OF VOLTAGE AT A0 PORT
unsigned int MAXIMUM = 1023; //MAXIMUM RESOLUTION
unsigned int MID = 512;
unsigned int MAX = 1023;


char buff[12];
int flag = 0;
float voltage = 0;


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "math.h"
#include <SPI.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete


unsigned int previousMillis = 0;        // will store last time LED was updated


const long interval = 10000;           // interval at which to blink (milliseconds) 5000
unsigned int currentMillis = 0;



void setup() {

  Serial.begin(4800);
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(Sw, INPUT);
  inputString.reserve(200);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.display();
  delay(2000);
  // Clear the buffer.
  display.clearDisplay();

}

void loop() {
 
  currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
      voltage = 0;
   


    display.clearDisplay();
    display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
    display.setTextSize(1.5);             // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);        // Draw white text
    display.setCursor(10, 5);
    display.println ("ABC System");
    display.setCursor(10, 15);
    display.println ("Started");
    display.setCursor(10, 25);
    display.println("V:");

    

    display.setCursor(10, 35);
    display.setTextSize(2);
    display.println(voltage);
    display.display();
    inputString = "";


    delay(1); //1 ms delay for stability
  }

 

  unsigned long tempA = 0; // MOTOR A motor speed value start from zero
  unsigned long tempA1 = 0; //FORMULA
  unsigned long tempA2 = 0; //FORMULA
  Aval = analogRead(VRx);
  tempA = Aval;
  Serial.println (tempA);




  unsigned long tempB = 0; // MOTOR B motor speed value start from zero
  unsigned long tempB1 = 0; // Formula
  unsigned long tempB2 = 0; // Formula
  Bval = analogRead(VRy);
  tempB = Bval;
  Serial.println (tempB);



  //FORWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 520) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((tempB - MID) * 100) / MID);
    char st[20]; // Send command to the buffer Vx as a statement for motor A
    char st1[20]; // Send command  Vy as a statement for motor B
    sprintf(st, "<A+%03lu>", tempB1);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);
  }


  //BACKWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB < 480) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((MID - tempB) * 100) / MID);
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempB1);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }

  //NEUTRAL DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 480 && tempB < 520) && (tempA > 480 && tempA < 520)) {
    char st[20];
    Serial.println ("<A+000>");
    Serial.println ("<B+000>");
  }

  ////PURE LEFT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  //if ((tempA<450) && (tempB>450 && tempB<550)){
  //  tempA1=(((MID-tempA)*100)/MID);
  // char st[20];
  // char st1[20];
  // sprintf(st1,"<B+%03lu>",tempA1);
  // Serial.println (st1);
  // }
  //
  // //PURE RIGHT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  // if ((tempA>550) && (tempB>450 && tempB<550))
  //  {
  //    tempA1 = (((tempA-MID)*100)/MID);
  //
  //  char st[20];
  // char st1[20];
  // sprintf(st,"<A+%03lu>",tempA1);
  // Serial.println (st);
  // }


  // FORWARD RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH AND RIGHT MOTOR SPEED IS LOW //DONE SUCCESSFULLY
  //=============================================================================================================//
  if ((tempA > 520) && tempB > 520)
  {
    tempA1 = ((tempB * 100) / MAX);
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 -  ((tempB1 * tempA1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA1);
    sprintf(st1, "<B+%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);
  }

  // FORWARD LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW AND RIGHT MOTOR SPEED IS HIGH //DONE SUCCESSFULLY
  //==========================================================================================================//
  if ((tempA < 480) && (tempB > 520))
  {
    tempB1 = ((tempB * 100) / MAX );
    tempA1 = (100 - ((tempA * 100) / (MID)));
    tempA2 = (tempB1 -  ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA2);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }



  // REVERSE RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE) AND RIGHT MOTOR SPEED IS LOW (MOVING CLOCKWISE)
  //====================================================================================================================================//

  if (tempA > 520 && tempB < 480)
  {
    tempA1 = (((MID - tempB) * 100) / (MID) );
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 - ((tempB1 * tempA1) / 100 ));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA1);
    sprintf(st1, "<B-%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);

  }


  // REVERSE LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW (MOVING CLOCKWISE) AND RIGHT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE)
  // ===================================================================================================================================//

  if (tempA < 480 && tempB < 480)
  {

    tempB1 = (((MID - tempB) * 100) / (MID));
    tempA1 = (100 - ((tempA * tempB1) / (MID)));
    tempA2 = (tempB1 - ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA2);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }




}

void serialEvent() {
 
  if (Serial.available() > 0) {
    inputString = Serial.readString();
    previousMillis = currentMillis;
    flag = 1;
  }


  String newstr = inputString.substring(inputString.indexOf(',') + 1, inputString.indexOf('V'));
  Serial.println(inputString);
  Serial.println(newstr);

  //Vstr.toCharArray(buff,12);
  voltage = newstr.toFloat();
  Serial.println(voltage);
  //  counter++;
  display.clearDisplay();
  display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
  display.setTextSize(1.5);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 5);
  display.println ("ABC System");
  display.setCursor(10, 15);
  display.println ("Started");
  display.setCursor(10, 25);
  display.println("V:");
  //
  

  display.setCursor(10, 35);
  display.setTextSize(2);
  display.println(voltage);
  display.display();
  inputString = "";




  delay(1); //1 ms delay for stability
}

Thank you so much.

Today You likely know what the different lines do in the code. You will not know that forever. For Your own best, and for helpers, to understand, please add lots of comments explaining what is done and why.

Now I have traced the problem ... I recently noticed that there is no problem with timers and delays, the actual problem I'm facing right now is that when I run motors with joystick it runs good. If I run again after 2 minutes then it is not running. When I pressed the reset button and then move the joystick then it runs ..

There is a problem in these lines of code

if (TaskNumber == 5 || TaskNumber == 7) {
      if (ADirection == FWD) {
        // MOTOR A INCREMENT FOR FORWARD DIRECTION
        if (AVelocity > 0 && AVelocity <= 10) {
          APwm = 1535 ;
        }
        else if (AVelocity > 10 && AVelocity <= 20) {
          APwm = 1540;
        }
        else if (AVelocity > 20 && AVelocity <= 30) {
          APwm = 1545;
        }
        else if (AVelocity > 30 && AVelocity <= 40) {
          APwm = 1550;
        }
        else if (AVelocity > 40 && AVelocity <= 50) {
          APwm = 1555;
        }
        else if (AVelocity > 50 && AVelocity <= 60) {
          APwm = 1560;
        }
        else if (AVelocity > 60 && AVelocity <= 70) {
          APwm = 1565;
        }
        else if (AVelocity > 70 && AVelocity <= 80) {
          APwm = 1570;
        }
        else if (AVelocity > 80 && AVelocity <= 90) {
          APwm = 1575;
        }
        else if (AVelocity > 90 && AVelocity <= 100) {
          APwm++ ;
        }
        else {
          APwm = 1500;
        }
        ESC_1.writeMicroseconds(APwm);
        AFwdFlag = 1;
      }

      else {
        if (AFwdFlag == 1)
        {
          ESC_1.writeMicroseconds(NEUTRAL); delay(100);
          ESC_1.writeMicroseconds(REVERSE); delay(100);
          ESC_1.writeMicroseconds(NEUTRAL); delay(100);
          AFwdFlag = 0;
        }
        // MOTOR A DECREMENT FOR BACKWARD DIRECTION
        if (AVelocity > 0 && AVelocity <= 10) {
          APwm = 1430 ;
        }
        else if (AVelocity > 10 && AVelocity <= 20) {
          APwm = 1420;
        }
        else if (AVelocity > 20 && AVelocity <= 30) {
          APwm = 1410;
        }
        else if (AVelocity > 30 && AVelocity <= 40) {
          APwm = 1400;
        }
        else if (AVelocity > 40 && AVelocity <= 50) {
          APwm = 1390;
        }
        else if (AVelocity > 50 && AVelocity <= 60) {
          APwm = 1380;
        }
        else if (AVelocity > 60 && AVelocity <= 70) {
          APwm = 1370;
        }
        else if (AVelocity > 70 && AVelocity <= 80) {
          APwm = 1360;
        }
        else if (AVelocity > 80 && AVelocity <= 90) {
          APwm = 1350;
        }
        else if (AVelocity > 90 && AVelocity <= 100) {
          APwm-- ;
        }
        else {
          BPwm = 1500;
        }
        ESC_1.writeMicroseconds(APwm);


      }//ELSE

    }



    if (TaskNumber == 6 || TaskNumber == 7) {
      if (BDirection == FWD) {
        // MOTOR B INCREMENT FOR FORWARD DIRECTION
        if (BVelocity > 0 && BVelocity <= 10) {
          BPwm = 1535 ;
        }
        else if (BVelocity > 10 && BVelocity <= 20) {
          BPwm = 1540;
        }
        else if (BVelocity > 20 && BVelocity <= 30) {
          BPwm = 1545;
        }
        else if (BVelocity > 30 && BVelocity <= 40) {
          BPwm = 1550;
        }
        else if (BVelocity > 40 && BVelocity <= 50) {
          BPwm = 1555;
        }
        else if (BVelocity > 50 && BVelocity <= 60) {
          BPwm = 1560;
        }
        else if (BVelocity > 60 && BVelocity <= 70) {
          BPwm = 1565;
        }
        else if (BVelocity > 70 && BVelocity <= 80) {
          BPwm = 1570;
        }
        else if (BVelocity > 80 && BVelocity <= 90) {
          BPwm = 1575;
        }
        else if (BVelocity > 90 && BVelocity <= 100) {
          BPwm++ ;
        }
        else {
          BPwm = 1500;
        }

        ESC_2.writeMicroseconds(BPwm);
        BFwdFlag = 1;
      }
      else {
        if (AFwdFlag == 1)
        {
          ESC_2.writeMicroseconds(NEUTRAL); delay(100);
          ESC_2.writeMicroseconds(REVERSE); delay(100);
          ESC_2.writeMicroseconds(NEUTRAL); delay(100);
          BFwdFlag = 0;
        }
        // MOTOR B DECREMENT FOR BACKWARD DIRECTION
        if (BVelocity > 0 && BVelocity <= 10) {
          BPwm = 1430 ;
        }
        else if (BVelocity > 10 && BVelocity <= 20) {
          BPwm = 1420;
        }
        else if (BVelocity > 20 && BVelocity <= 30) {
          BPwm = 1410;
        }
        else if (BVelocity > 30 && BVelocity <= 40) {
          BPwm = 1400;
        }
        else if (BVelocity > 40 && BVelocity <= 50) {
          BPwm = 1390;
        }
        else if (BVelocity > 50 && BVelocity <= 60) {
          BPwm = 1380;
        }
        else if (BVelocity > 60 && BVelocity <= 70) {
          BPwm = 1370;
        }
        else if (BVelocity > 70 && BVelocity <= 80) {
          BPwm = 1360;
        }
        else if (BVelocity > 80 && BVelocity <= 90) {
          BPwm = 1350;
        }
        else if (BVelocity > 90 && BVelocity <= 100) {
          BPwm-- ;
        }
        else {
          BPwm = 1500;
        }
        ESC_2.writeMicroseconds(BPwm);


      }//ELSE
    }


    TaskNumber = 0;
  }
}

Please post the entire code. The reason for the hanging is most likely in the code You cut away.

this one is the complete code at the receiving side with motors.

int VRx = A1; // ANALOG READING FOR LEFT AND RIGHT MOVEMENT FOR X-AXIS
int VRy = A0; // ANALOG READING FOR UPWARD AND DOWNWARD MOVEMENT FOR Y-AXIS
int Sw = 2; //
float Aval; //VALUE OF VOLTAGE AT A1PORT
float Bval; //VALUE OF VOLTAGE AT A0 PORT
unsigned int MAXIMUM = 1023; //MAXIMUM RESOLUTION
unsigned int MID = 512;
unsigned int MAX = 1023;


char buff[12];
int flag = 0;
float voltage = 0;


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "math.h"
#include <SPI.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete


unsigned int previousMillis = 0;        // will store last time LED was updated


const long interval = 10000;           // interval at which to blink (milliseconds) 5000
unsigned int currentMillis = 0;



void setup() {

  Serial.begin(4800);
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(Sw, INPUT);
  inputString.reserve(200);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.display();
  delay(2000);
  // Clear the buffer.
  display.clearDisplay();

}

void loop() {
 
  currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
      voltage = 0;
   


    display.clearDisplay();
    display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
    display.setTextSize(1.5);             // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);        // Draw white text
    display.setCursor(10, 5);
    display.println ("ABC System");
    display.setCursor(10, 15);
    display.println ("Started");
    display.setCursor(10, 25);
    display.println("V:");

    

    display.setCursor(10, 35);
    display.setTextSize(2);
    display.println(voltage);
    display.display();
    inputString = "";


    delay(1); //1 ms delay for stability
  }

 

  unsigned long tempA = 0; // MOTOR A motor speed value start from zero
  unsigned long tempA1 = 0; //FORMULA
  unsigned long tempA2 = 0; //FORMULA
  Aval = analogRead(VRx);
  tempA = Aval;
  Serial.println (tempA);




  unsigned long tempB = 0; // MOTOR B motor speed value start from zero
  unsigned long tempB1 = 0; // Formula
  unsigned long tempB2 = 0; // Formula
  Bval = analogRead(VRy);
  tempB = Bval;
  Serial.println (tempB);



  //FORWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 520) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((tempB - MID) * 100) / MID);
    char st[20]; // Send command to the buffer Vx as a statement for motor A
    char st1[20]; // Send command  Vy as a statement for motor B
    sprintf(st, "<A+%03lu>", tempB1);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);
  }


  //BACKWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB < 480) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((MID - tempB) * 100) / MID);
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempB1);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }

  //NEUTRAL DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 480 && tempB < 520) && (tempA > 480 && tempA < 520)) {
    char st[20];
    Serial.println ("<A+000>");
    Serial.println ("<B+000>");
  }

  ////PURE LEFT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  //if ((tempA<450) && (tempB>450 && tempB<550)){
  //  tempA1=(((MID-tempA)*100)/MID);
  // char st[20];
  // char st1[20];
  // sprintf(st1,"<B+%03lu>",tempA1);
  // Serial.println (st1);
  // }
  //
  // //PURE RIGHT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  // if ((tempA>550) && (tempB>450 && tempB<550))
  //  {
  //    tempA1 = (((tempA-MID)*100)/MID);
  //
  //  char st[20];
  // char st1[20];
  // sprintf(st,"<A+%03lu>",tempA1);
  // Serial.println (st);
  // }


  // FORWARD RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH AND RIGHT MOTOR SPEED IS LOW //DONE SUCCESSFULLY
  //=============================================================================================================//
  if ((tempA > 520) && tempB > 520)
  {
    tempA1 = ((tempB * 100) / MAX);
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 -  ((tempB1 * tempA1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA1);
    sprintf(st1, "<B+%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);
  }

  // FORWARD LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW AND RIGHT MOTOR SPEED IS HIGH //DONE SUCCESSFULLY
  //==========================================================================================================//
  if ((tempA < 480) && (tempB > 520))
  {
    tempB1 = ((tempB * 100) / MAX );
    tempA1 = (100 - ((tempA * 100) / (MID)));
    tempA2 = (tempB1 -  ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA2);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }



  // REVERSE RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE) AND RIGHT MOTOR SPEED IS LOW (MOVING CLOCKWISE)
  //====================================================================================================================================//

  if (tempA > 520 && tempB < 480)
  {
    tempA1 = (((MID - tempB) * 100) / (MID) );
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 - ((tempB1 * tempA1) / 100 ));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA1);
    sprintf(st1, "<B-%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);

  }


  // REVERSE LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW (MOVING CLOCKWISE) AND RIGHT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE)
  // ===================================================================================================================================//

  if (tempA < 480 && tempB < 480)
  {

    tempB1 = (((MID - tempB) * 100) / (MID));
    tempA1 = (100 - ((tempA * tempB1) / (MID)));
    tempA2 = (tempB1 - ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA2);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }




}

void serialEvent() {
 
  if (Serial.available() > 0) {
    inputString = Serial.readString();
    previousMillis = currentMillis;
    flag = 1;
  }


  String newstr = inputString.substring(inputString.indexOf(',') + 1, inputString.indexOf('V'));
  Serial.println(inputString);
  Serial.println(newstr);

  //Vstr.toCharArray(buff,12);
  voltage = newstr.toFloat();
  Serial.println(voltage);
  //  counter++;
  display.clearDisplay();
  display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
  display.setTextSize(1.5);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 5);
  display.println ("ABC System");
  display.setCursor(10, 15);
  display.println ("Started");
  display.setCursor(10, 25);
  display.println("V:");
  //
  

  display.setCursor(10, 35);
  display.setTextSize(2);
  display.println(voltage);
  display.display();
  inputString = "";




  delay(1); //1 ms delay for stability
}

This one is at the transmit side that control the motors with joystick

When the trouble had started "You see, in serial monitor that commands being sent".
Add a serial print in the receiver showing if commands are received.

Which Arduino is used?

Using strings might be the reason. Free memory gets fragmentized.

Im using an arduino UNO on transmitting side with Joystick and Arduino mega2560 at the receiving side with the motors. I also tried an arduino mega at transmitted side as well but same issue i'm facing; I also tried to change the serial port from Serial0 to Serial1 but facing the same issue ... So I am back to Arduino UNO at the transmitted side with the joystick.

Yes, for first time run, the motors run perfectly and when I move the position of joystick to the neutral side then the motors stop and after 1 to 2 minutes, when I moved the joystick position then the motors won't run although the values are changing on a serial monitor with respect to the joystick movement.

The last sentence, a quote: values are changing on a serial monitor.
Be more precise. Is that S M showing transmitter output or receiver received data?

JOYSTICK values are changing on a serial monitor at the transmitter side.

When the motors won't run then I close the serial monitor and open it up again so the motors run with respect to the joystick movements.

Yes, the commands are received as well on a serial monitor

In simple words, I need to reset the arduino uno at the transmitter side every time when I want to run the motors with a joystick ...

Closing and opening serial monitor creates a reset in the controller.

It looks like the transmitter is the troubeling one.
When it hangs no commands are shown on the transmitter serial monitor?

When You compile the transmitter code, check the amount if memory used.

Again, strings are often a reason for failures like this.

No, the commands are showing on the transmitter serial monitor and I also see the values are changes when I move the joystick but the motors won't run.

I traced out the problem is at the Joystick code ...

Please see this, in the end I have cleared the input string " inputString = " so that's why I am not facing the problem when I send the commands through serial monitor.

I also need to clear char st [20] & char st1[20]; so that there will be no issues ...
Please help me out regarding this ..

Well done finding the inputString cleared!

st1[20] = ""; ?? Do it at the proper occasion...

Yes, I tried this one but still same problem exist. It complies successfully but it gives me this message as well while compilation

That's a warning, not an error. Try using the ASCII char null, nill as a char

Now I found the actual problem why it causes, why the arduino needs to reset everytime ... Because of too much Serial.println(st) and Serial.println(st1) included in every direction of the code its causes the buffer full ... SerialPrint is necessary in every direction, is there any way to include st and st1 for Serial Print in one line?