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();
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()?
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 ?