Using an OLED Display to show live Motor Speed

Hello, I wrote a code that allows me to change the speed of a motor with a rotary encoder. Everything works fine, but when I try to display the speed on an OLED display, the motor moves very slowly and the display shows only small values. What should I do?

//OLED

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>


U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 

//Encoder 

#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>

Encoder myEnc(7, 8);

//Stepper

#include <AccelStepper.h>
AccelStepper stepper(1, 3, 4);

//
//
//


void setup() {
  
u8g2.begin();
u8g2.clearBuffer();      

  stepper.setMaxSpeed(3000); //2000
  stepper.setSpeed(50);

}

long position  = -1999;
long pulses;
//
//
//

void loop() {

 stepper.runSpeed();
 long newPos = myEnc.read();

if (newPos != position) {
    position = newPos;
    }
    
    
if (position >= -60 && position <= 60) { //position bewteen -40 and 40
 
  pulses = position*1;
       
    }

if (position >= -60 && position < -60 || position > 60 && position <= 80) { //position bewteen -80 and -40 or same in positive
 
   pulses = position*1.4;
       
    }

if (position < -80 || position > 80) {

  pulses = position*20;
  
       
    }
stepper.setSpeed(pulses);


   u8g2.clearBuffer();         // clear the internal memory  

   u8g2.setFont(u8g2_font_logisoso16_tf );  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
   u8g2.setCursor(34,32);
   u8g2.println(pulses); // write something to the internal memory

   u8g2.sendBuffer();         // transfer internal memory to the display

}

Hi KaruYT, and welcome.

OLED displays are drawn, not written.
The difference is that every dot has to be set, and that takes some time.
You are clearing your display buffer, and filling it in every iteration of 'loop'.
That takes a lot of time.
So don't do that.
You do not need to clear all content of your screen, only the part that needs to be updated.
And that also implies only if there is a changed displayed value.

So no need to draw the same content all the time.
And don't clear and rewrite the entire screen.
It'll speed up your sketch.
If that still doesn't do the trick, you need to rethink the way your sketch runs so that the stepper motor can be controlled more constantly.
If that means your display's content is updated in a less smoothly manner, so be it.

@KaruYT

The AccelStepper library requires polling for each step and the display update takes too long, so this slows the step rate.

With an UNO/Nano/Mini Pro I use interrupts on Timer 1 for the stepper speed so no polling is needed.

I am guessing you are using a Teensy 2.0, so my stepper code will not work but you may well find an interrupt based steeper library somewhere or use the library here.

KaruYT:
Everything works fine, but when I try to display the speed on an OLED display, the motor moves very slowly and the display shows only small values. What should I do?

//OLED

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
........
}

The display is probably more useful written rather than drawn, but I don't know if that will fix the problem. You might find this example useful. I don't know anything about Arduino.h, it is likely to be redundant, and it may be part of the problem.

@bodmer

I'm using a Nano, so if you have a working code I am really interested

@KaruYT

I have posted my stepper motor test sketch here.

It was originally used to test a motor driving one axis of a small CNC milling machine.

The sketch is for an A4988 driver using enable, clock and direction signals but could be adapted for other drivers.

Hope it helps.

Hello Bodmer

I have the dabe issue but with a countdown timer... can you help in finding a way to fix it as i am totally lost..´

https://forum.arduino.cc/index.php?topic=676231.45

thank you
xmodpt