Real Time 2 Stepper Motors Control using NodeMCU and Blynk

I wanted to control 2 Stepper Motors for running a robot using the joystick of Blynk App and NodeMCU. But when I searched for the codes of real time controlling of Stepper Motors online I didn’t get much code and most of them were not real time.

This is code I am currently working on:-

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

#define RightMotorSpeed D7
#define RightMotorDir   D8  

const int enPin = D2;
const int enPin2 = D3;

#define LeftMotorSpeed  D6  
#define LeftMotorDir    D5


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// Use your own WiFi settings
char auth[] = "LRTCZUnCI06P-pqh5rlPXRbuOUgQ_uGH";
char ssid[] = "Airtel_7599998800";
char pass[] = "air71454";

// neutral zone settings for x and y
// joystick must move outside these boundary numbers to activate the motors
// makes it a little easier to control the wifi car
int minRange = 312;
int maxRange = 712;

// analog speeds from 0 (lowest) - 1023 (highest)
// 3 speeds used -- 0 (noSpeed), 350 (minSpeed), 850 (maxSpeed).
// use whatever speeds you want...too fast made it a pain in the ass to control
int minSpeed = 450;
int maxSpeed = 1023;
int noSpeed = 0;


void moveControl(int x, int y)
{
  // movement logic
  // move forward

   // y je vetsi jak maxrange a současně x je vetsi jak minRange a současne mensi jak max range 
  while(y >= maxRange && x >= minRange && x <= maxRange) //zataci R
  {
    digitalWrite(RightMotorDir,HIGH);  
    digitalWrite(LeftMotorDir,HIGH);

    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);

    delayMicroseconds(500);

    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);

    delayMicroseconds(500);
  }

  // move forward right
  while(x >= maxRange && y >= maxRange)   //zataci R
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
   analogWrite(RightMotorSpeed,minSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
  }

  // move forward left
  while(x <= minRange && y >= maxRange)
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,minSpeed);
  }

  // neutral zone
  while(y < maxRange && y > minRange && x < maxRange && x > minRange)
  {
    analogWrite(RightMotorSpeed,noSpeed); 
    analogWrite(LeftMotorSpeed,noSpeed);
  }

 // move back
  while(y <= minRange && x >= minRange && x <= maxRange)
  {
    digitalWrite(RightMotorDir,LOW);
    digitalWrite(LeftMotorDir,LOW);
   analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
  }

  // move back and right
 while(y <= minRange && x <= minRange)
  {
   digitalWrite(RightMotorDir,LOW);
    digitalWrite(LeftMotorDir,LOW);
    analogWrite(RightMotorSpeed,minSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);  
  }

  // move back and left
  while(y <= minRange && x >= maxRange)
  {
    digitalWrite(RightMotorDir,LOW);
    digitalWrite(LeftMotorDir,LOW);
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,minSpeed);
  }
}

void setup()
{
  // initial settings for motors off and direction forward
  pinMode(RightMotorSpeed, OUTPUT);
  pinMode(LeftMotorSpeed, OUTPUT);
  pinMode(RightMotorDir, OUTPUT);
  pinMode(LeftMotorDir, OUTPUT);
  digitalWrite(RightMotorSpeed, LOW);
  digitalWrite(LeftMotorSpeed, LOW);
  digitalWrite(RightMotorDir, HIGH);
  digitalWrite(LeftMotorDir,HIGH);



    Serial.begin(9600);

    pinMode(enPin,OUTPUT);
    digitalWrite(enPin,LOW);

    pinMode(enPin2,OUTPUT);
    digitalWrite(enPin2,LOW);



  Blynk.begin(auth, ssid, pass);
 }


void loop()
{
  Blynk.run();
}


BLYNK_WRITE(V1)
{
  int x = param[0].asInt();
  int y = param[1].asInt();
  moveControl(x,y); 
}

Here I have defined by 2 Stepper motors as Right and Left and since I am using TB6600 Motor Driver therefore their Pulse and Direction Pins are also Defined. That is the main reason that I am unable to use the Stepper motor Library for the code.

Running the code I see that Both the motors runs fine once for 3 to 5 seconds and the the Blynk Server Disconnects and Reconnects again causing the motors to stop and not creating a real time communication. Some one Please help me create a code for these 2 stepper motors to run at realtime.

I think that Blink.run() causes the server to reconnect and stop the motor.

I also searched for this cause and found that instead of Stepper motor Library I should use AccelStepper Library but that is also not achieved . Please help me with this. Any correct reference is also appreciable. Thanks in advance.

That’s because while your moveControl function is executing you are preventing the program flow from returning to ‘void loop’. Without Blynk.run() being called frequently two things will happen:

  • Changes to your joystick widget will bot be reflected in your code
  • The Blynk server won’t be receiving heartbeat handshakes from the device, so it will decide that it’s MIA.

A quick and dirty solution is to add multiple Blynk.run() commands into your ‘moveControl’ function. I’d add one before and after each delay() command, but a better option would be to remove the delays completely.

Pete.

1 Like

I did the same thing as you advised and only first ran the forward command for testing. But now only 1 motor is running and that also for infinite time even when I turn the joystick to standby position

This is the edited code

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
 
#define RightMotorSpeed D7
#define RightMotorDir   D8  

const int enPin = D2;
const int enPin2 = D3;

#define LeftMotorSpeed  D6  
#define LeftMotorDir    D5
 
 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// Use your own WiFi settings
char auth[] = "LRTCZUnCI06P-pqh5rlPXRbuOUgQ_uGH";
char ssid[] = "Airtel_7599998800";
char pass[] = "air71454";
 
// neutral zone settings for x and y
// joystick must move outside these boundary numbers to activate the motors
// makes it a little easier to control the wifi car
int minRange = 312;
int maxRange = 712;
 
// analog speeds from 0 (lowest) - 1023 (highest)
// 3 speeds used -- 0 (noSpeed), 350 (minSpeed), 850 (maxSpeed).
// use whatever speeds you want...too fast made it a pain in the ass to control
int minSpeed = 450;
int maxSpeed = 1023;
int noSpeed = 0;
 
 
void moveControl(int x, int y)
{
  // movement logic
  // move forward

   // y je vetsi jak maxrange a současně x je vetsi jak minRange a současne mensi jak max range 
  while(y >= maxRange && x >= minRange && x <= maxRange) //zataci R
  {
    digitalWrite(RightMotorDir,HIGH);  
    digitalWrite(LeftMotorDir,HIGH);
    
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
    
    Blynk.run();
    
    delayMicroseconds(500);

    Blynk.run();
    
    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);
    
    Blynk.run();
    
    delayMicroseconds(500);
  
    Blynk.run();
    
    }
/****************************************************************************************************************** 
  // move forward right
  while(x >= maxRange && y >= maxRange)   //zataci R
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
   analogWrite(RightMotorSpeed,minSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
  }
 
  // move forward left
  while(x <= minRange && y >= maxRange)
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,minSpeed);
  }
 
  // neutral zone
  while(y < maxRange && y > minRange && x < maxRange && x > minRange)
  {
    analogWrite(RightMotorSpeed,noSpeed); 
    analogWrite(LeftMotorSpeed,noSpeed);
  }
 
 // move back
  while(y <= minRange && x >= minRange && x <= maxRange)
  {
    digitalWrite(RightMotorDir,LOW);
    digitalWrite(LeftMotorDir,LOW);
   analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
  }
 
  // move back and right
 while(y <= minRange && x <= minRange)
  {
   digitalWrite(RightMotorDir,LOW);
    digitalWrite(LeftMotorDir,LOW);
    analogWrite(RightMotorSpeed,minSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);  
  }
 
  // move back and left
  while(y <= minRange && x >= maxRange)
  {
    digitalWrite(RightMotorDir,LOW);
    digitalWrite(LeftMotorDir,LOW);
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,minSpeed);
  }

*************************************************************************************/ 
}
void setup()
{
  // initial settings for motors off and direction forward
  pinMode(RightMotorSpeed, OUTPUT);
  pinMode(LeftMotorSpeed, OUTPUT);
  pinMode(RightMotorDir, OUTPUT);
  pinMode(LeftMotorDir, OUTPUT);
  digitalWrite(RightMotorSpeed, LOW);
  digitalWrite(LeftMotorSpeed, LOW);
  digitalWrite(RightMotorDir, HIGH);
  digitalWrite(LeftMotorDir,HIGH);



    Serial.begin(9600);

    pinMode(enPin,OUTPUT);
    digitalWrite(enPin,LOW);
 
    pinMode(enPin2,OUTPUT);
    digitalWrite(enPin2,LOW);


 
  Blynk.begin(auth, ssid, pass);
 }
 
 
void loop()
{
  Blynk.run();
}
 
 
BLYNK_WRITE(V1)
{
  int x = param[0].asInt();
  int y = param[1].asInt();
  moveControl(x,y); 
}

Do you not think that this has somethi g to do with this code being commented out…

Pete.

Hello Peter Sir,
Due to your help it has started to run both the stepper motors. I am really thankful to you for that but sir it is not either comming back to neutral state nor to other motions please also help here.

This is the current code

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
 
#define RightMotorSpeed D7
#define RightMotorDir   D8  

const int enPin = D2;
const int enPin2 = D3;

#define LeftMotorSpeed  D6  
#define LeftMotorDir    D5
 
 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// Use your own WiFi settings
char auth[] = "LRTCZUnCI06P-pqh5rlPXRbuOUgQ_uGH";
char ssid[] = "Airtel_7599998800";
char pass[] = "air71454";
 
// neutral zone settings for x and y
// joystick must move outside these boundary numbers to activate the motors
// makes it a little easier to control the wifi car
int minRange = 312;
int maxRange = 712;
 
// analog speeds from 0 (lowest) - 1023 (highest)
// 3 speeds used -- 0 (noSpeed), 350 (minSpeed), 850 (maxSpeed).
// use whatever speeds you want...too fast made it a pain in the ass to control
int minSpeed = 600;
int maxSpeed = 1023;
int noSpeed = 0;
 
 
void moveControl(int x, int y)
{
  // movement logic
  ////////////////////////////// move forward/////////////////////////////////////////

   // y je vetsi jak maxrange a současně x je vetsi jak minRange a současne mensi jak max range 
  while(y >= maxRange && x >= minRange && x <= maxRange) //zataci R
  {
    digitalWrite(RightMotorDir,HIGH);  
    digitalWrite(LeftMotorDir,HIGH);
    
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
    
    Blynk.run();
    
    delayMicroseconds(500);

    Blynk.run();
    
    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);
    
    Blynk.run();
    
    delayMicroseconds(500);
  
    Blynk.run();
    
    }

  //////////////////////////////// move forward right ////////////////////////////////////
  while(x >= maxRange && y >= maxRange)   //zataci R
  {
  //  digitalWrite(RightMotorDir,HIGH);
  //  digitalWrite(LeftMotorDir,HIGH);
  //  analogWrite(RightMotorSpeed,minSpeed); 
  //  analogWrite(LeftMotorSpeed,maxSpeed);
  

    digitalWrite(RightMotorDir,HIGH);  
    digitalWrite(LeftMotorDir,HIGH);
    
    analogWrite(RightMotorSpeed,minSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
    
    Blynk.run();
    
    delayMicroseconds(500);

    Blynk.run();
    
    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);
    
    Blynk.run();
    
    delayMicroseconds(500);
  
    Blynk.run();
   
  }
 
 //////////////////////////////////// move forward left //////////////////////////////////
  while(x <= minRange && y >= maxRange)
  {
  //  digitalWrite(RightMotorDir,HIGH);
  //  digitalWrite(LeftMotorDir,HIGH);
  //  analogWrite(RightMotorSpeed,maxSpeed); 
  //  analogWrite(LeftMotorSpeed,minSpeed);
  

    digitalWrite(RightMotorDir,HIGH);  
    digitalWrite(LeftMotorDir,HIGH);
    
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,minSpeed);
    
    Blynk.run();
    
    delayMicroseconds(500);

    Blynk.run();
    
    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);
    
    Blynk.run();
    
    delayMicroseconds(500);
  
    Blynk.run();
   
  
  }

/////////////////////////////////// neutral zone//////////////////////////////////
  while(y < maxRange && y > minRange && x < maxRange && x > minRange)
  {
    analogWrite(RightMotorSpeed,0); 
    analogWrite(LeftMotorSpeed,noSpeed);
  }

 //////////////////////////// move back////////////////////////////////////////
  while(y <= minRange && x >= minRange && x <= maxRange)
  {
  //  digitalWrite(RightMotorDir,LOW);
  //  digitalWrite(LeftMotorDir,LOW);
  //  analogWrite(RightMotorSpeed,maxSpeed); 
  //  analogWrite(LeftMotorSpeed,maxSpeed);
 

    digitalWrite(RightMotorDir,LOW);  
    digitalWrite(LeftMotorDir,LOW);
    
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
    
    Blynk.run();
    
    delayMicroseconds(500);

    Blynk.run();
    
    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);
    
    Blynk.run();
    
    delayMicroseconds(500);
  
    Blynk.run();
   
  }
 
 //////////////////// move back and right////////////////////////////
 while(y <= minRange && x <= minRange)
  {
  //  digitalWrite(RightMotorDir,LOW);
  //  digitalWrite(LeftMotorDir,LOW);
  //  analogWrite(RightMotorSpeed,minSpeed); 
  //  analogWrite(LeftMotorSpeed,maxSpeed);  
 
  

    digitalWrite(RightMotorDir,LOW);  
    digitalWrite(LeftMotorDir,LOW);
    
    analogWrite(RightMotorSpeed,minSpeed); 
    analogWrite(LeftMotorSpeed,maxSpeed);
    
    Blynk.run();
    
    delayMicroseconds(500);

    Blynk.run();
    
    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);
    
    Blynk.run();
    
    delayMicroseconds(500);
  
    Blynk.run();
   
  }
 
  ///////////////////////// move back and left////////////////////////////////////////
  while(y <= minRange && x >= maxRange)
  {
  //  digitalWrite(RightMotorDir,LOW);
  //  digitalWrite(LeftMotorDir,LOW);
  //  analogWrite(RightMotorSpeed,maxSpeed); 
  // analogWrite(LeftMotorSpeed,minSpeed);
  
  
  
    digitalWrite(RightMotorDir,LOW);  
    digitalWrite(LeftMotorDir,LOW);
    
    analogWrite(RightMotorSpeed,maxSpeed); 
    analogWrite(LeftMotorSpeed,minSpeed);
    
    Blynk.run();
    
    delayMicroseconds(500);

    Blynk.run();
    
    digitalWrite(RightMotorSpeed,0); 
    digitalWrite(LeftMotorSpeed,0);
    
    Blynk.run();
    
    delayMicroseconds(500);
  
    Blynk.run();
   
  }

 
}
void setup()
{
  // initial settings for motors off and direction forward
  pinMode(RightMotorSpeed, OUTPUT);
  pinMode(LeftMotorSpeed, OUTPUT);
  pinMode(RightMotorDir, OUTPUT);
  pinMode(LeftMotorDir, OUTPUT);
  digitalWrite(RightMotorSpeed, LOW);
  digitalWrite(LeftMotorSpeed, LOW);
  digitalWrite(RightMotorDir, HIGH);
  digitalWrite(LeftMotorDir,HIGH);



    Serial.begin(9600);

    pinMode(enPin,OUTPUT);
    digitalWrite(enPin,LOW);
 
    pinMode(enPin2,OUTPUT);
    digitalWrite(enPin2,LOW);


 
  Blynk.begin(auth, ssid, pass);
 }
 
 
void loop()
{
  Blynk.run();
}
 
 
BLYNK_WRITE(V1)
{
  int x = param[0].asInt();
  int y = param[1].asInt();
  moveControl(x,y); 
}

Hello Everyone, I have started to control the 2 stepper motors full control from forward movement to backward and left to right but now only one problem is that how could I run these motors using Blynk Joystick using NodeMCU. I am currently running them without Blynk App with this code

#include <AccelStepper.h>
 
long receivedMMdistance = 0; //distance in mm from the computer
long receivedDelay = 0; //delay between two steps, received from the computer
long receivedAcceleration = 0; //acceleration value from computer

long receivedMMdistance_2 = 0; //distance in mm from the computer
long receivedDelay_2 = 0; //delay between two steps, received from the computer
long receivedAcceleration_2 = 0; //acceleration value from computer

char receivedCommand; //character for commands
/* s = Start (CCW) // needs steps and speed values
 * o = open (CCW) // needs steps and speed values
 * c = close (CW) //needs steps and speed values
 * a = set acceleration // needs acceleration value
 * n = stop right now! // just the 'n' is needed
 */
 
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
 
 
 
// direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper(1, D8, D7);
AccelStepper stepper_2(1, D5, D6);
 
 
void setup()
{
  Serial.begin(9600); //define baud rate
  Serial.println("Testing Accelstepper"); //print a message
 
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(2000); //SPEED = Steps / second
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
 
 // stepper.disableOutputs(); //disable outputs, so the motor is not getting warm (no current)
 
 
  //setting up some default values for maximum speed and maximum acceleration
  stepper_2.setMaxSpeed(2000); //SPEED = Steps / second
  stepper_2.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
 
 // stepper_2.disableOutputs(); //disable outputs, so the motor is not getting warm (no current)
 
 
}
 
void loop()
{
 
  checkSerial(); //check serial port for new commands
 
  continuousRun2(); //method to handle the motor
 
}
 
 
void continuousRun2() //method for the motor
{
  if (runallowed == true)
  {
    if (abs(stepper.currentPosition()) < receivedMMdistance) //abs() is needed because of the '<'
    {
      stepper.enableOutputs(); //enable pins
      stepper.run(); //step the motor (this will step the motor by 1 step at each loop)
    }
    else //program enters this part if the required distance is completed
    {
     
      runallowed = false; //disable running -> the program will not try to enter this if-else anymore
      stepper.disableOutputs(); // disable power
      Serial.print("POS: ");
      Serial.println(stepper.currentPosition()); // print pos -> this will show you the latest relative number of steps
      stepper.setCurrentPosition(0); //reset the position to zero
      Serial.print("POS: ");
      Serial.println(stepper.currentPosition()); // print pos -> this will show you the latest relative number of steps; we check here if it is zero for real
    }


 
    if (abs(stepper_2.currentPosition()) < receivedMMdistance) //abs() is needed because of the '<'
    {
      stepper_2.enableOutputs(); //enable pins
      stepper_2.run(); //step the motor (this will step the motor by 1 step at each loop)
    }
    else //program enters this part if the required distance is completed
    {
     
      runallowed = false; //disable running -> the program will not try to enter this if-else anymore
      stepper_2.disableOutputs(); // disable power
      Serial.print("POS: ");
      Serial.println(stepper_2.currentPosition()); // print pos -> this will show you the latest relative number of steps
      stepper_2.setCurrentPosition(0); //reset the position to zero
      Serial.print("POS: ");
      Serial.println(stepper_2.currentPosition()); // print pos -> this will show you the latest relative number of steps; we check here if it is zero for real
    }
 
  }
  else //program enters this part if the runallowed is FALSE, we do not do anything
  {
    return;
 
  }
}
 
void checkSerial() //method for receiving the commands
{  
  //switch-case would also work, and maybe more elegant
 
  if (Serial.available() > 0) //if something comes
  {
    receivedCommand = Serial.read(); // this will read the command character
    newData = true; //this creates a flag
  }
 
  if (newData == true) //if we received something (see above)
  {
    
////////////////////////////FORWARD COMMAND///////////////////////////
if (receivedCommand == 't') //this is the measure part
    {
      //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
      runallowed = true; //allow running
     
 
      receivedMMdistance = Serial.parseFloat(); //value for the steps
      receivedDelay = Serial.parseFloat(); //value for the speed
      int neg_receivedMMdistance = receivedMMdistance * -1;
      receivedDelay_2 = receivedDelay;
      receivedMMdistance_2 = receivedMMdistance;
      
   // Serial.print("receivedDelay_2 => "); Serial.print(receivedDelay_2); Serial.print(" receivedMMdistance_2 => ");Serial.println(receivedMMdistance_2);
            
      Serial.print(receivedMMdistance); //print the values for checking
      Serial.print(receivedDelay);
      Serial.println("Measure "); //print the action
      stepper.setMaxSpeed(receivedDelay); //set speed
      stepper.move(neg_receivedMMdistance); //set distance

      stepper_2.setMaxSpeed(receivedDelay_2); //set speed
      stepper_2.move(receivedMMdistance_2); //set distance 
    
    
    }
    
////////////////////////////BACKWARD COMMAND///////////////////////////
if (receivedCommand == 'g') //this is the measure part
    {
      //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
      runallowed = true; //allow running
     
 
      receivedMMdistance = Serial.parseFloat(); //value for the steps
      receivedDelay = Serial.parseFloat(); //value for the speed

      receivedDelay_2 = receivedDelay;
      receivedMMdistance_2 = receivedMMdistance;
     int neg_receivedMMdistance_2 = receivedMMdistance_2 * -1;
      
   // Serial.print("receivedDelay_2 => "); Serial.print(receivedDelay_2); Serial.print(" receivedMMdistance_2 => ");Serial.println(receivedMMdistance_2);
            
      Serial.print(receivedMMdistance); //print the values for checking
      Serial.print(receivedDelay);
      Serial.println("Measure "); //print the action
      stepper.setMaxSpeed(receivedDelay); //set speed
      stepper.move(receivedMMdistance); //set distance

      stepper_2.setMaxSpeed(receivedDelay_2); //set speed
      stepper_2.move(neg_receivedMMdistance_2); //set distance 
    }
    
////////////////////////////FORWARD RIGHT COMMAND///////////////////////////
if (receivedCommand == 'y') //this is the measure part
    {
      //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
      runallowed = true; //allow running
     
 
      receivedMMdistance = Serial.parseFloat(); //value for the steps
      receivedDelay = Serial.parseFloat(); //value for the speed
      int neg_receivedMMdistance = receivedMMdistance * -1;
      long half_receivedDelay = (receivedDelay / 5);

   // Serial.print("Half Value => "); Serial.println(half_receivedDelay);
   
      receivedDelay_2 = receivedDelay;
      receivedMMdistance_2 = receivedMMdistance;

   // Serial.print("receivedDelay_2 => "); Serial.print(receivedDelay_2); Serial.print(" receivedMMdistance_2 => ");Serial.println(receivedMMdistance_2);
            
      Serial.print(receivedMMdistance); //print the values for checking
      Serial.print(receivedDelay);
      Serial.println("Measure "); //print the action
      stepper.setMaxSpeed(half_receivedDelay); //set speed
      stepper.move(neg_receivedMMdistance); //set distance

      stepper_2.setMaxSpeed(receivedDelay_2); //set speed
      stepper_2.move(receivedMMdistance_2); //set distance 
    }
////////////////////////////FORWARD LEFT COMMAND///////////////////////////
if (receivedCommand == 'r') //this is the measure part
    {
      //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
      runallowed = true; //allow running
     
 
      receivedMMdistance = Serial.parseFloat(); //value for the steps
      receivedDelay = Serial.parseFloat(); //value for the speed
      int neg_receivedMMdistance = receivedMMdistance * -1;

      receivedDelay_2 = receivedDelay;
      receivedMMdistance_2 = receivedMMdistance;
      long half_receivedDelay_2 = (receivedDelay_2 / 5);
      
   // Serial.print("receivedDelay_2 => "); Serial.print(receivedDelay_2); Serial.print(" receivedMMdistance_2 => ");Serial.println(receivedMMdistance_2);
            
      Serial.print(receivedMMdistance); //print the values for checking
      Serial.print(receivedDelay);
      Serial.println("Measure "); //print the action
      stepper.setMaxSpeed(receivedDelay); //set speed
      stepper.move(neg_receivedMMdistance); //set distance

      stepper_2.setMaxSpeed(half_receivedDelay_2); //set speed
      stepper_2.move(receivedMMdistance_2); //set distance 
    }
    
////////////////////////////BACKWARD RIGHT COMMAND///////////////////////////
if (receivedCommand == 'h') //this is the measure part
    {
      //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
      runallowed = true; //allow running
     
 
      receivedMMdistance = Serial.parseFloat(); //value for the steps
      receivedDelay = Serial.parseFloat(); //value for the speed
      long half_receivedDelay = (receivedDelay / 5);

   // Serial.print("Half Value => "); Serial.println(half_receivedDelay);
   
      receivedDelay_2 = receivedDelay;
      receivedMMdistance_2 = receivedMMdistance;
      int neg_receivedMMdistance_2 = receivedMMdistance_2 * -1;

   // Serial.print("receivedDelay_2 => "); Serial.print(receivedDelay_2); Serial.print(" receivedMMdistance_2 => ");Serial.println(receivedMMdistance_2);
            
      Serial.print(receivedMMdistance); //print the values for checking
      Serial.print(receivedDelay);
      Serial.println("Measure "); //print the action
      stepper.setMaxSpeed(half_receivedDelay); //set speed
      stepper.move(receivedMMdistance); //set distance

      stepper_2.setMaxSpeed(receivedDelay_2); //set speed
      stepper_2.move(neg_receivedMMdistance_2); //set distance 
    }
////////////////////////////BAKCWARD LEFT COMMAND///////////////////////////
if (receivedCommand == 'f') //this is the measure part
    {
      //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
      runallowed = true; //allow running
     
 
      receivedMMdistance = Serial.parseFloat(); //value for the steps
      receivedDelay = Serial.parseFloat(); //value for the speed

      receivedDelay_2 = receivedDelay;
      receivedMMdistance_2 = receivedMMdistance;
      int neg_receivedMMdistance_2 = receivedMMdistance_2 * -1;
      long half_receivedDelay_2 = (receivedDelay_2 / 5);
      
   // Serial.print("receivedDelay_2 => "); Serial.print(receivedDelay_2); Serial.print(" receivedMMdistance_2 => ");Serial.println(receivedMMdistance_2);
            
      Serial.print(receivedMMdistance); //print the values for checking
      Serial.print(receivedDelay);
      Serial.println("Measure "); //print the action
      stepper.setMaxSpeed(receivedDelay); //set speed
      stepper.move(receivedMMdistance); //set distance

      stepper_2.setMaxSpeed(half_receivedDelay_2); //set speed
      stepper_2.move(neg_receivedMMdistance_2); //set distance 
    }
/////////////////////////////////////////////////////////////////////////////////////    
   
    //STOP - STOP
    if (receivedCommand == 'n') //immediately stops the motor
    {
      runallowed = false; //disable running
       
      stepper.setCurrentPosition(0); // reset position
      Serial.println("STOP "); //print action
      stepper.stop(); //stop motor
      stepper.disableOutputs(); //disable power
 
    }
 
    //SET ACCELERATION
    if (receivedCommand == 'a') //Setting up a new acceleration value
    {
      runallowed = false; //we still keep running disabled, since we just update a variable
     
      receivedAcceleration = Serial.parseFloat(); //receive the acceleration from serial
 
      stepper.setAcceleration(receivedAcceleration); //update the value of the variable
 
      Serial.println("ACC Updated "); //confirm update by message
 
    }
 
  }
  //after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again.
  newData = false;
 
 
}