Stepper motor control with I2C-LCD display

Hello all.

I would like to help me with a problem that is quite common.
Interferences between LCD and stepper motors.

I am a beginner in arduino and therefore I would need a little help.
I am working on a project realized for some time by another user, which I would like to improve. (https://forum.arduino.cc/index.php?topic=586382.0)

This circuit that i’ve made should spin a stepper motor one way or the other, and, on the LCD, display on the first line the RPM and on the second, the direction of the motor.

In my circuit i added an i2C LCD. I want the LCD to display the following:
-the direction of the stepper motor (Left-Stop-Right(3 way switch))
-RPM of the motor instead of percentage of the potentiometer

When the motor is stopped(switch to the mid position) i want it to be deactivated(without power).
I want to increase the max motor speed, but keep the minimum.

My main problem is that when i activete (delete the comment) on the lines of code for the display(what to print), my stepper motor spins VERY slowly. (My stepper is: 42BYGH34 and driver A4988)

How can i solve these problems?Any tips?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MsTimer2.h>

//Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
//LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2); //Change to (0x27,16,2) for 16x2 LCD.

// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int switchPinA = A1;
const int switchPinB = A2;
int customDelay, customDelayMapped; // Defines variables
const int ms1 = 8;
const int ms2 = 9;
const int ms3 = 10;
int switchPinAVal = 0;
int switchPinBVal = 0;
//const int stepsPerRevolution = 200;       //dar
//int dim = stepsPerRevolution;    //cip    //dar

void setup() {
  {
   //Initiate the LCD
  lcd.init();                    // initialize the lcd
  lcd.backlight();               // turn on backlight
  lcd.begin(16, 2);              //cip
  lcd.print("Speed:");           //cip
  lcd.setCursor(11, 0);          //cip
  lcd.print("n/min");            //cip
  lcd.setCursor(0, 1);           //cip
  lcd.print("Direction:");       //cip
}
  
Serial.begin(9600);              //cip
  
  pinMode(switchPinA, INPUT_PULLUP);
  pinMode(switchPinB, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  //pinMode(ms1, OUTPUT);
  //pinMode(ms2, OUTPUT);
  //pinMode(ms3, OUTPUT);

}
void loop() 
{
 // myStepper.step(dim);              //cip  
 // void Direction();                 //cip  //dar

  if (switchPinAVal == HIGH && switchPinBVal == LOW)
  {
    motorRunClockwise();
    //dim = stepsPerRevolution;                 //dar
    lcd.setCursor(10, 1);             //cip         
    lcd.print(">>>>>>");              //cip
    lcd.print(millis());              //cip
  }
  if (switchPinAVal == LOW && switchPinBVal == HIGH)
  {
    motorRunAntiClockwise();
//    dim = -stepsPerRevolution;      //cip
    //lcd.setCursor(10, 1);           //cip         
    //lcd.print("<<<<<<");            //cip
    //lcd.print(millis());            //cip
  }
  //else
 if (switchPinAVal == HIGH && switchPinBVal == HIGH || switchPinAVal == LOW && switchPinBVal == LOW)     //cip
  {
    digitalWrite(stepPin, LOW); // stop the motor
    //lcd.setCursor(10, 1);           //cip
    //lcd.print(" STOP ");            //cip
    //lcd.print(millis());            //cip
  }

  switchPinAVal = digitalRead(A1);
  switchPinBVal = digitalRead(A2);
}

///////////////////////////////////// Function for reading the Potentiometer
int speedControl() {

  int customDelay = analogRead(A0); // Reads the potentiometer
   int newCustom = map(customDelay, 0, 1023, 0, 4000); // Converts the read values of the potentiometer from 0 to 1023 into desireded delay values (0 to 150)
 //int newCustom = map(customDelay, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)

  //myStepper.setSpeed(customDelay);                      //cip
  //lcd.setCursor(6, 0);                                  //cip
  //lcd.print(float(float(customDelay) / float(200)));    //cip

  return newCustom; //dar
}

////////////////////////////////////
void motorRunClockwise() {
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, LOW);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

////////////////////////////////////
void motorRunAntiClockwise() {
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, HIGH);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom… :slight_smile:

I replied as follows to your earlier Post in the old Thread - the merging seems not to have worked

You need to post your program.

It sounds as if you are updating the LCD too often. A few times per second should be sufficient and should not interfere with the stepper motor. It would also be a good idea to send as little data as possible to the LCD because writing to the LCD (or to Serial) is slow.

...R

Here is:

I understand but I don't know how to do this.

I would like to do something like this: min. 0:55

https://www.youtube.com/watch?v=iufFYjnj1c4&list=PLivgXfHIaqIPl57E8_EmTpFCH6tUFUc9r&index=27&t=156s

Robin2: I replied as follows to your earlier Post in the old Thread - the merging seems not to have worked

...R

Hi,
OPs circuit;


Tom... :slight_smile:

Hi,

switchPinAVal = digitalRead(A1);
switchPinBVal = digitalRead(A2);

Can you put this at the START of the loop to make it more logical? Also use the variables you have setup.

switchPinAVal = digitalRead(switchPinA);
switchPinBVal = digitalRead(switchPinB);

Thanks.. Tom...

Also, it appears you have a change over switch (SPDT) which is wired to connect either A1 or A2 to ground, however you have neither on board pullup resistors nor a pinMode() statement to apply the internal pin pullup resistors.

Further, you appear to be controlling the stepper motor by a variable delay in the loop which can be from 0 to 8mS per loop iteration. It may be better to set up a timer to control the motor so the activities in the loop() (reading the potentiometer, writing to the display etc. ) have no impact on the motor and vice versa.

I made the changes indicated. I left in the code only the following 2 active lines

lcd.setCursor(10, 1);             //cip         
lcd.print(">>>>>>");           //cip

, stopping the initial ones but for nothing.

// lcd.print("Speed:");           //cip
//  lcd.setCursor(11, 0);          //cip
//  lcd.print("n/min");            //cip
//  lcd.setCursor(0, 1);           //cip
//  lcd.print("Direction:");       //cip

I do not know what to do anymore.

Hi, Can you post us your updated code with the edits we have suggested? Do you have a DMM?

Thanks.. Tom... :)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MsTimer2.h>

//Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
//LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2); //Change to (0x27,16,2) for 16x2 LCD.

// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int switchPinA = A1;
const int switchPinB = A2;
int customDelay, customDelayMapped; // Defines variables
const int ms1 = 8;
const int ms2 = 9;
const int ms3 = 10;
int switchPinAVal = 0;
int switchPinBVal = 0;
//const int stepsPerRevolution = 200;       //dar
//int dim = stepsPerRevolution;    //cip    //dar

void setup() {
  {
   //Initiate the LCD
  lcd.init();                    // initialize the lcd
  lcd.backlight();               // turn on backlight
  lcd.begin(16, 2);              //cip
  lcd.print("Speed:");           //cip
  lcd.setCursor(11, 0);          //cip
  lcd.print("n/min");            //cip
  lcd.setCursor(0, 1);           //cip
  lcd.print("Direction:");       //cip
}
  
Serial.begin(9600);              //cip
  
  pinMode(switchPinA, INPUT_PULLUP);
  pinMode(switchPinB, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ms1, OUTPUT);
  pinMode(ms2, OUTPUT);
  pinMode(ms3, OUTPUT);

}
void loop() 
{
  switchPinAVal = digitalRead(switchPinA);
  switchPinBVal = digitalRead(switchPinB);
  
 // myStepper.step(dim);              //cip  
 // void Direction();                 //cip  //dar

  if (switchPinAVal == HIGH && switchPinBVal == LOW)
  {
    motorRunClockwise();
    //dim = stepsPerRevolution;                 //dar
    //lcd.setCursor(10, 1);             //cip         
    //lcd.print(">>>>>>");              //cip
    //lcd.print(millis());              //cip
  }
  if (switchPinAVal == LOW && switchPinBVal == HIGH)
  {
    motorRunAntiClockwise();
    //dim = -stepsPerRevolution;      //cip
    //lcd.setCursor(10, 1);           //cip         
    //lcd.print("<<<<<<");            //cip
    //lcd.print(millis());            //cip
  }
  //else
 if (switchPinAVal == HIGH && switchPinBVal == HIGH || switchPinAVal == LOW && switchPinBVal == LOW)     //cip
  {
    digitalWrite(stepPin, LOW); // stop the motor
    //lcd.setCursor(10, 1);           //cip
    //lcd.print(" STOP ");            //cip
    //lcd.print(millis());            //cip
  }

  }

///////////////////////////////////// Function for reading the Potentiometer
int speedControl() {

  int customDelay = analogRead(A0); // Reads the potentiometer
   int newCustom = map(customDelay, 0, 1023, 0, 4000); // Converts the read values of the potentiometer from 0 to 1023 into desireded delay values (0 to 150)
 //int newCustom = map(customDelay, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)

  //myStepper.setSpeed(customDelay);                      //cip
  //lcd.setCursor(6, 0);                                  //cip
  //lcd.print(float(float(customDelay) / float(200)));    //cip
  //lcd.print(millis());            //cip

  return newCustom; //dar
}

////////////////////////////////////
void motorRunClockwise() {
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, LOW);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

////////////////////////////////////
void motorRunAntiClockwise() {
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, HIGH);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

This is my last version of code, and an image with my setup.

!(http://c:\Users\VirtualPax\Downloads\WhatsApp Image 2020-03-22 at 18.11.19.jpg)

Hi,
Thanks for the pic.


Tom... :slight_smile:

so... can someone help me with what i should write in my code to make it run as it should?

Hi, Why do you have an extra set of {} in the void setup() , highlighted in RED?

void setup() { { //Initiate the LCD lcd.init(); // initialize the lcd lcd.backlight(); // turn on backlight lcd.begin(16, 2); //cip lcd.print("Speed:"); //cip lcd.setCursor(11, 0); //cip lcd.print("n/min"); //cip lcd.setCursor(0, 1); //cip lcd.print("Direction:"); //cip }

Serial.begin(9600); //cip

pinMode(switchPinA, INPUT_PULLUP); pinMode(switchPinB, INPUT_PULLUP); pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(ms1, OUTPUT); pinMode(ms2, OUTPUT); pinMode(ms3, OUTPUT);

}

Remove those brackets and see what you code runs like then. Tom... :)

I'd probably user Timer1 to control the stepper so it is take out of the loop then there should be no timing interference between the motor and the LCD-

Create a global variable motorControl and set it instead of calling functions motorRunClockwise() and motorRunAntiClockwise(). Say 0 = stop, 1 = clockwise, 2 = anticlockwise.

In the loop, call speedControl() and use the result obtained to set the period of Timer1, something like: Timer1.setPeriod( speedControl() ) ; // microseconds

Create an ISR for Timer1. Something like:

void motorController() {

 static bool toggle = false ; // we make two entries here per cycle

 if ( motorControl == 0 ) {
   // handle stop here
 }
 else {
   if ( toggle == false ) {
     digitalWrite(stepPin, HIGH);
   }
   else {
     if ( motorControl == 1 ) {
       // clockwise
       digitalWrite(dirPin, LOW);
     }
     else {
       // anticlockwise (motorControl == 2 )
       digitalWrite(dirPin, HIGH);
     }
     digitalWrite(ms1, HIGH);
     digitalWrite(ms2, HIGH);
     digitalWrite(ms3, HIGH);
     digitalWrite(stepPin, LOW);
   }
   toggle = ! toggle ;
 }
}

In setup(), connect the ISR to Timer1, something like: Timer1.attachInterrupt( motorController ) ;

@6v6gt since i am new to arduino and i don’t know what i am doing, can you PLEASE implement that in my code? I have no idea where i should put the code you sent me, how to declare things and what to keep or discard from my code. I would appreciate a lot if you could do that for me

i modified the library for the LCD ( #include <hd44780.h> ). i heard that this one is “faster”

@TomGeorge i made the modifications you suggested, but with no success

// -------------- Include the following Librarys -------------- //
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>


// -------------- Connect to LCD via I2C -------------- //
// Define the LCD  Being used and its settings //
hd44780_I2Cexp lcd1(0x27);


// -------------- Defines pins numbers -------------- //
const int stepPin = 3;
const int dirPin = 4;
const int switchPinA = A1;
const int switchPinB = A2;
int customDelay, customDelayMapped; // Defines variables
const int ms1 = 8;
const int ms2 = 9;
const int ms3 = 10;
int switchPinAVal = 0;
int switchPinBVal = 0;
//int motorControl =


//--------------------------MIN & MAX Potentiometer  Settings-------------------------------//
// Define our maximum and minimum speed in steps per second (scale pot to these) //

//const int  MAX_SPEED = 3000; //Max potentiometer speed, 2000 steps per second divide by 200 steps (1 revolution)=10 revultions per second,=600rpm.  Geared at 3:1 this =200rpm per min on the PM727, that is 2 inchs per minute travel
//const int MIN_SPEED = 8; //Min potentiometer speed

void setup() {

  Serial.begin(9600);              //cip   - Comunicare cu Serial monitor

  // -------------- Initiate the LCD --------------  //
  lcd1.init();                    // initialize the lcd
  lcd1.backlight();               // turn on backlight
  lcd1.begin(16, 2);              //cip

  // On power up display on the LCD intro
  lcd1.print("Speed:");           //cip
  lcd1.setCursor(11, 0);          //cip
  lcd1.print("n/min");            //cip
  lcd1.setCursor(0, 1);           //cip
  lcd1.print("Direction:");       //cip
  delay(5000);

  pinMode(switchPinA, INPUT_PULLUP);
  pinMode(switchPinB, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ms1, OUTPUT);
  pinMode(ms2, OUTPUT);
  pinMode(ms3, OUTPUT);
}


// -------------- Begin Loop , Begin Operating Machine -------------- //
void loop()
{
  switchPinAVal = digitalRead(switchPinA);
  switchPinBVal = digitalRead(switchPinB);

}

// -------------- Function for reading the Potentiometer -------------- //
int speedControl() {

  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 0, 4000); // Converts the read values of the potentiometer from 0 to 1023 into desireded delay values (0 to 3000)
  
  lcd1.setCursor(6, 0);                                      //cip
  lcd1.print(float(float(customDelay) / float(200)));    //cip , 2

  return newCustom; //dar
}

////////////////////////////////////
void motorController() {

  static bool toggle = false ; // we make two entries here per cycle

  if ( motorControl == 0 ) {
    // handle stop here
  }
  else {
    if ( toggle == false ) {
      digitalWrite(stepPin, HIGH);
    }
    else {
      if ( motorControl == 1 ) {
        // clockwise
        digitalWrite(dirPin, LOW);
      }
      else {
        // anticlockwise (motorControl == 2 )
        digitalWrite(dirPin, HIGH);
      }
      digitalWrite(ms1, HIGH);
      digitalWrite(ms2, HIGH);
      digitalWrite(ms3, HIGH);
      digitalWrite(stepPin, LOW);
    }
    toggle = ! toggle ;
  }
}

OK. I can put something together for you. I just want to make sure before I start that the code that you last posted (with all the LCD statements commented out) does work in that the motor can go clockwise, anticlockwise at various speeds and also stop. Also, I can't test it so any troubleshooting could get complicated but we can try.

this is the last version of my code that somewhat works. It works perfectly untill i uncomment from the loop the “lcd1.setCursor” and “lcd1.print” commands. When those commands are activated, the potentiometer does not seem to work and the stepper motor spins extremely slow.

Another problem i noticed just now is that the potentiometer values are inverted on the lcd screen( if the pot is at max, on the lcd is at min)

// -------------- Include the following Librarys -------------- //
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>


// -------------- Connect to LCD via I2C -------------- //
//Define the LCD  Being used and its settings
hd44780_I2Cexp lcd1(0x27);


// -------------- Defines pins numbers -------------- //
const int stepPin = 3;
const int dirPin = 4;
const int switchPinA = A1;
const int switchPinB = A2;
int customDelay, customDelayMapped; // Defines variables
const int ms1 = 8;
const int ms2 = 9;
const int ms3 = 10;
int switchPinAVal = 0;
int switchPinBVal = 0;


//--------------------------MIN & MAX Potentiometer  Settings-------------------------------//
     // Define our maximum and minimum speed in steps per second (scale pot to these) //

void setup() {
  
       // -------------- Initiate the LCD --------------  //
    lcd1.init();                    // initialize the lcd
    lcd1.backlight();               // turn on backlight
    lcd1.begin(16, 2);              //cip
    
    // -------------- On power up display on the LCD intro -------------- //
    lcd1.print("Speed:");           //cip
    lcd1.setCursor(11, 0);          //cip
    lcd1.print("n/min");            //cip
    lcd1.setCursor(0, 1);           //cip
    lcd1.print("Direction:");       //cip
      
  pinMode(switchPinA, INPUT_PULLUP);
  pinMode(switchPinB, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ms1, OUTPUT);
  pinMode(ms2, OUTPUT);
  pinMode(ms3, OUTPUT);

}


// -------------- Begin Loop , Begin Operating Machine -------------- //
void loop()
{
  switchPinAVal = digitalRead(switchPinA);
  switchPinBVal = digitalRead(switchPinB);

  
    if (switchPinAVal == HIGH && switchPinBVal == LOW)
  {
    motorRunClockwise();

    // -------------- Update LCD to ">>>>>>" -------------- //
    //lcd1.setCursor(10, 1);             //cip
    //lcd1.print(">>>>>>");              //cip
    
  }
  if (switchPinAVal == LOW && switchPinBVal == HIGH)
  {
    motorRunAntiClockwise();

    // -------------- Update LCD to "<<<<<<" -------------- //
    //lcd1.setCursor(10, 1);           //cip
    //lcd1.print("<<<<<<");            //cip
   
  }
  //else
  if (switchPinAVal == HIGH && switchPinBVal == HIGH || switchPinAVal == LOW && switchPinBVal == LOW)     //cip
  {
    digitalWrite(stepPin, LOW); // stop the motor


    // -------------- Update LCD to "STOP" -------------- //
    //lcd1.setCursor(10, 1);           //cip
    //lcd1.print(" STOP ");            //cip
    
  }

}

// -------------- Function for reading the Potentiometer -------------- //
  int speedControl() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 0, 4000); // Converts the read values of the potentiometer from 0 to 1023 into desireded delay values (0 to 4000)
  
   // -------------- Update LCD to "Potentiometer value" -------------- //
  //lcd1.setCursor(6, 0);                                  //cip
  //lcd1.print(float(float(customDelay) / float(200)), 2);    //cip , 2
  

  return newCustom; //dar
}

/////////////////////////////////////////////////////////////////////////
void motorRunClockwise() {
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, LOW);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

////////////////////////////////////////////////////////////////////////
void motorRunAntiClockwise() {
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, HIGH);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

This should work at least like the old one did. I have not tested it with a stepper motor but the rest appears to work. If you have problems with the stepper motor, describe carefully what you see.

// -------------- Include the following Librarys -------------- //
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

#include <TimerOne.h>


// -------------- Connect to LCD via I2C -------------- //
//Define the LCD  Being used and its settings
hd44780_I2Cexp lcd1(0x27);


// -------------- Defines pins numbers -------------- //
const int stepPin = 3;
const int dirPin = 4;
const int switchPinA = A1;
const int switchPinB = A2;
int customDelay, customDelayMapped; // Defines variables
const int ms1 = 8;
const int ms2 = 9;
const int ms3 = 10;
int switchPinAVal = 0;
int switchPinBVal = 0;

enum class MotorControl { CLOCKWISE, ANTICLOCKWISE, STOP } ;  //MD1
volatile MotorControl motorControl = MotorControl::STOP ;    //initial state  //MD1


//--------------------------MIN & MAX Potentiometer  Settings-------------------------------//
// Define our maximum and minimum speed in steps per second (scale pot to these) //

void setup() {

  Serial.begin( 115200 ) ;    //MD1
  Serial.println(F("Motor Controller")) ; //MD1

  

  // -------------- Initiate the LCD --------------  //
  lcd1.init();                    // initialize the lcd
  lcd1.backlight();               // turn on backlight
  lcd1.begin(16, 2);              //cip

  // -------------- On power up display on the LCD intro -------------- //
  lcd1.print("Speed:");           //cip
  lcd1.setCursor(11, 0);          //cip
  lcd1.print("n/min");            //cip
  lcd1.setCursor(0, 1);           //cip
  lcd1.print("Direction:");       //cip

  pinMode(switchPinA, INPUT_PULLUP);
  pinMode(switchPinB, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ms1, OUTPUT);
  pinMode(ms2, OUTPUT);
  pinMode(ms3, OUTPUT);

  Timer1.initialize( 5000 ); //start 5mS
  Timer1.attachInterrupt( motorController ) ;  //MD1  
}


// -------------- Begin Loop , Begin Operating Machine -------------- //
void loop()
{

  //Serial.println("in loop") ;
  //delay(1000) ;

  
  switchPinAVal = digitalRead(switchPinA);
  switchPinBVal = digitalRead(switchPinB);


  if (switchPinAVal == HIGH && switchPinBVal == LOW)
  {
    // motorRunClockwise(); //MD1
    motorControl = MotorControl::CLOCKWISE ;  //MD1

    // -------------- Update LCD to ">>>>>>" -------------- //
    lcd1.setCursor(10, 1);             //cip
    lcd1.print(">>>>>>");              //cip

  }
  if (switchPinAVal == LOW && switchPinBVal == HIGH)
  {
    // motorRunAntiClockwise();
    motorControl = MotorControl::ANTICLOCKWISE ;  //MD1

    // -------------- Update LCD to "<<<<<<" -------------- //
    lcd1.setCursor(10, 1);           //cip
    lcd1.print("<<<<<<");            //cip

  }
  //else
  if (switchPinAVal == HIGH && switchPinBVal == HIGH || switchPinAVal == LOW && switchPinBVal == LOW)     //cip
  {
    motorControl = MotorControl::STOP ;  //MD1
    digitalWrite(stepPin, LOW); // stop the motor


    // -------------- Update LCD to "STOP" -------------- //
    lcd1.setCursor(10, 1);           //cip
    lcd1.print(" STOP ");            //cip

  }

  static int speedControlUsLast = -1 ;
  int speedControlUs = speedControl() ;
  if ( speedControlUs < 500 ) speedControlUs = 500 ; // can't be too low
  if ( speedControlUs != speedControlUsLast ) {
    Timer1.setPeriod(  speedControl() ) ;  // microseconds
    speedControlUsLast = speedControlUs ;
  }

}



void motorController() {

  static bool toggle = false ; // we make two entries here per cycle

  if ( motorControl == MotorControl::STOP ) {
    // handle stop here
  }
  else {
    if ( toggle == false ) {
      digitalWrite(stepPin, HIGH);
    }
    else {
      if ( motorControl == MotorControl::CLOCKWISE ) {
        // clockwise
        digitalWrite(dirPin, LOW);
      }
      else if ( motorControl == MotorControl::ANTICLOCKWISE ) {
        digitalWrite(dirPin, HIGH);
      }
      else {
        // should not happen
      }
      digitalWrite(ms1, HIGH);
      digitalWrite(ms2, HIGH);
      digitalWrite(ms3, HIGH);
      digitalWrite(stepPin, LOW);
    }
    toggle = ! toggle ;
  }
}








// -------------- Function for reading the Potentiometer -------------- //
int speedControl() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 0, 4000); // Converts the read values of the potentiometer from 0 to 1023 into desireded delay values (0 to 4000)

  // -------------- Update LCD to "Potentiometer value" -------------- //
  lcd1.setCursor(6, 0);                                  //cip
  lcd1.print(float(float(customDelay) / float(200)), 2);    //cip , 2


  return newCustom; //dar
}

/////////////////////////////////////////////////////////////////////////
void motorRunClockwise() {
  //NOT USED
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, LOW);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

////////////////////////////////////////////////////////////////////////
void motorRunAntiClockwise() {
  //NOT USED
  customDelayMapped = speedControl();
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(dirPin, HIGH);
  digitalWrite(ms1, HIGH);
  digitalWrite(ms2, HIGH);
  digitalWrite(ms3, HIGH);
}

i uploaded the code into my arduino board. The motor speed is PERFECT, but the potentiometer does not work, the display is frozen and the motor does not switch direction nor stop. Here is a link with a short video i made:

https://youtu.be/AvvwoTiLVKE