Stepper motor Looping issue NEMA 17 Stepper motor

Hello,

I am using NEMA 17 Stepper motor with DRV8825 Driver
My goal is to Adjust the speed of the stepper motor using a Potentiometer
and Display the speed in the OLED display (For telescope tracker)

This is my code

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.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);

#include <Stepper.h>
#define STEPS 200
#define dirPin 2
#define stepPin 3
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
Stepper stepper(STEPS, 2, 3);
#define motorInterfaceType 1
int Pval = 0;
int potVal = 0;

void setup() {
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); 
  }
  // Set the maximum speed in steps per second:
  stepper.setSpeed(1500);
  Serial.begin(9600);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  display.clearDisplay();
  
}
void loop() {
  display.clearDisplay();
  potVal = map(analogRead(A0), 0, 1024, 0, 1602);
  stepper.setSpeed(potVal);
    display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 20);            // Start at top-left corner
  display.println(potVal);
    display.display();
  stepper.step(100);
  
}

The code works but

Here I am experiencing some Jerk at intervals (Not a smooth rotation)
due to this issue, it is also not going to its max speed ,My guesses are these lines are taking time to complete because without these lines (OLED printing) it works flawlessly but this feature is mandatory for me

    display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 20);            // Start at top-left corner
  display.println(potVal);
    display.display();

Here is the video link for reference - Arduino stepper Jerk.mp4 - Google Drive

I have tried to solve it for some hours now so any help is greatly appreciated. Thanks everybody in advance!

Did you comment out those line of code and try your program and did the motor stop jerking?

Thank you so much for the response. Yeah it stopped jerking after commenting that line of code @Paul_KD7HB

But i need to print the speed of the stepper motor in OLED display

Do you need to clear the display and then print all the fixed data each time through loop()? Or do you really need to just print data if it has changed from the last time through loop()?

I just need to just print data if it has changed from the last time through loop() @Paul_KD7HB

Then, that should help a whole lot.

But not completely. It will yerk less often, but still writing to the OLED will interrupt creating step pulses.

@VishanAmarnath : You could try using the MoToStepper class of my MobaTools library. This library creates the steps in timer interrupts, which will work while writing to the display ( if that lib doesn't disable interrupts ).

Could you help me with MobaTools because it is so different and hard to understand, could you please help me with what example or which part of it I have to use for my application ?

Try this as a first run. Because I don't have your display, I cannot test it.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <MobaTools.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);

#include <Stepper.h>
#define STEPS 200
#define dirPin 2
#define stepPin 3
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
MoToStepper stepper(STEPS,STEPDIR);
//#define motorInterfaceType 1
int Pval = 0;
int potVal = 0;

void setup() {
  Serial.begin(9600);
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); 
  }
  stepper.attach( dirPin, stepPin );
  // Set the initial speed and ramp in steps per second:
  stepper.setSpeedSteps(15000, 200); // Speed is steps/10sec
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  display.clearDisplay();
  stepper.rotate(1);     // start rotating the stepper
  
}
void loop() {
  display.clearDisplay();
  potVal = map(analogRead(A0), 0, 1024, 0, 16020); // speed is in steps/10sec
  stepper.setSpeedSteps(potVal);
    display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 20);            // Start at top-left corner
  display.println(potVal);
    display.display();
}

Thank you so much, I will try it right now

Independent from the stepper lib you should consider @Paul_KD7HB's suggestion and update the display only when changes are made.

Thank you so much for you help @MicroBahner and @Paul_KD7HB

But the MobaTools didn't work so I completed the project without a display but still any solutions are appreciated

Thank you