Stepper motor + rotary encoder + oled -- display while stepper turn

Hello,

I have a problem with my current project :
I want to make a stepper motor turn and control the speed with a rotary encoder + display the number of turn made by the motor on a i2c oled display.

I use a SSD1306 oled and A4988 stepper driver.

The speed control work great with the non blocking Encoder library + the Accelstepper library.

But the problem i have is when i try to display the number of turn, the time spend by the arduino to print on the oled screen stop the motor a fraction of second so my motor doesn't run smoothly...

How i can fix that ? is it possible to print on a Oled screen while controlling a stepper motor ?

Thanks for your help.

My code :

#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"

#define I2C_ADDRESS 0x3C

SSD1306AsciiAvrI2c oled;

#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>

#include <AccelStepper.h>
AccelStepper stepper(1, 9, 8);
 
Encoder myEnc(2, 3);
b
int TurnsDone = 0;

void setup() {
  oled.begin(&Adafruit128x64, I2C_ADDRESS);
  oled.setFont(Adafruit5x7);

  Serial.begin(9600);
  oled.clear();

  stepper.setMaxSpeed(1000);
  stepper.setSpeed(50);
}

long position  = -999;


void loop() {
  stepper.runSpeed();


  long newPos = myEnc.read();
  if (newPos != position) {
    position = newPos;
    Serial.println(position);
  }
  stepper.setSpeed(position*10);


  if (stepper.currentPosition() == TurnsDone * 200)
  {
    oled.clear();
    oled.print(TurnsDone);
  }

  TurnsDone = stepper.currentPosition() / 200;
  
}

How i can fix that ? is it possible to print on a Oled screen while controlling a stepper motor ?

Yes. But, you've seen what happens when you try to that.

You need to determine whether it is printing to the OLED that takes the time, or if clearing the OLED is what takes the time.

If it is clearing the OLED that takes the time, stop clearing the OLED.

If you print "149" to the OLED, and then print "27", you'll see "279" displayed, right?

But, if you print "149 " and then print "27 ", you'll see just "27 ".

OK so i tried to not clear the oled, its run smooth and no more motor blocking but it print something like that :

1111122222333334444455555

I also tried to not print but just clear and the problem comes back.

So it is indeed the clear function that take too much time and stop the motor.

Soooo now how to solve this problem ? :smiley:

thanks

Soooo now how to solve this problem ?

There must be some function that allows you to define WHERE on the OLED to print the string. I imagine that that is a pretty quick function.

Yes i can define where print with

oled.setCursor(0,0);

But the problem comes back, and every turn it makes a mini engine stop.

:frowning:

R3mi:
Yes i can define where print with
But the problem comes back, and every turn it makes a mini engine stop.

:frowning:

If setCursor() is too slow, your only solution is to get a second Arduino to attach the OLED to, and let it do all the slow stuff.

I switched to a SPI OLED screen.
It's not perfect but really better. (without clearing)

Maybe if i switch to a segment display it will solve the problem ?
Or a DC motor with a hall effect sensor ?

Thanks for your help.

Or a DC motor with a hall effect sensor ?

That depends on why you are using a motor at all. You haven't described the whole project.