stepper motor with display speed

All,
I am driving a motor to drive the blade angle adjust on a table saw. if it comes out to be a success(already is, kinda) it will save me absolutely no time or effort, but I’m doing it anyway. Just a project to give my brain something technical to do.

Hardware: Nano to TB600 driver to Nema 23 motor. Switches into digital pins. Attempting to use a 4-digit, 7-segment display (tm1637/3462BS) to read out angle as a function of the number of steps.

Whenever the code sends data to the display, it drastically slows the stepping (while) loop down. I’ve got a few if statements in there and some math, but this does not appear to be the issue: the code functions pretty normally through if I comment out the TM1637 “display.clear…” and “display.shownumber…” commands in the “if” statement. Just looking for a way to send the data to a display that doesn’t slow the code down.
Could try different libraries, different display units, or sending data to a 2nd arduino, but am looking for advice, comments, direction as to the best thing to try next. Is there a way to send the data to the display outside the stepping loop?

The println commands also slow things down a just a bit, but these are just there as a debugger for now, will be commented out or deleted as things develop. The code is in early stages, want to get this issue solved before doing any dialing in of angle settings, backlash compensation, etc. The first while loop is the one i’m using for experimenting.
The “speedtimer” variable math just allows a slow start on the motor, takes the delay from 900 down to 500 ms, makes bumping the motor to an angle a bit easier.

Thanks in advance,
-w

#include <TM1637Display.h>
// Define the connections pins:
#define CLK 2
#define DIO 3
// Create display object of type TM1637Display:
TM1637Display display = TM1637Display(CLK, DIO);

//switch pins
const int vertpin = 11;
const int anglepin = 12;

//stepper driver pins
const int enablepin = 4;
const int dirpin = 5;
const int steppin = 7;

//stepper code parameters
int steps = 0; //counter for tracking the number of steps

//digital readout variables
const float stepsdeg = 311.111; //motor steps per degree of saw angle change
float doffset; //offset to counter backlash
int pos;
float deg = 0;
unsigned degr = 0;


void setup() {
  // Sets the pins modes
  pinMode(steppin, OUTPUT);
  pinMode(dirpin, OUTPUT);
  pinMode(enablepin, OUTPUT);
  digitalWrite(enablepin, HIGH);
  pinMode(vertpin, INPUT);
  pinMode(anglepin, INPUT);
  Serial.begin(250000);
  display.setBrightness(4);
  display.clear();

}
void loop() {
  int speedtimer = 900;
  while (digitalRead(anglepin) == HIGH) {
    digitalWrite(enablepin, LOW);
    digitalWrite(dirpin, HIGH);

    digitalWrite(steppin, HIGH);
    delayMicroseconds(speedtimer);
    digitalWrite(steppin, LOW);
    delayMicroseconds(speedtimer);

    if (speedtimer > 500) {
      speedtimer = speedtimer - 1;
    }
    steps = steps + 1;
    deg = steps / stepsdeg * 100 + doffset; //calculate and move decimal point to format
    degr = round(deg);
    unsigned lchr = (degr / 1U) % 10;//get last character to see if it's 0
    String degrst = String(degr);
    int leng = degrst.length();
    int pchr;
    if (lchr == 9) pchr = lchr;

    if ((lchr == 0) and (pchr == 9)) {
      if (leng == 1) pos = 2;
      else if (leng == 2) pos = 2;
      else if (leng == 3) pos = 1;
      else if (leng == 4) pos = 0;
      else degr = "err";
      display.clear();
      display.showNumberDecEx(degr, 0b11100000, true, leng, pos);
      Serial.println((String)"degr= " + degr + " length= " + leng + " pos= " + pos);
      Serial.println((String)"lchr= " + lchr + " pchr= " + pchr);
    }

  }
  while (digitalRead(vertpin) == HIGH) {
    digitalWrite(enablepin, LOW);
    digitalWrite(dirpin, LOW);

    digitalWrite(steppin, HIGH);
    delayMicroseconds(speedtimer);
    digitalWrite(steppin, LOW);
    delayMicroseconds(speedtimer);

    if (speedtimer > 500) {
      speedtimer = speedtimer - 1;
    }
    steps = steps - 1;
    double deg = steps / stepsdeg + doffset;
    display.clear();
    display.showNumberDecEx(deg, 0b11100000, false, 4, 0);
    Serial.println((String)"deg: " + deg + "  steps: " + steps);

  }
  digitalWrite(enablepin, HIGH);
}

The problem is that you are using delayMicroseconds() to control the speed of the motor. The Arduino can do nothing else during a delay() or delayMicroseconds(). However if you use millis() (or micros() ) to manage timing like in the second example in this Simple Stepper Code the updating of the display can be interleaved with the steps.

Also note how my example uses a very short step pulse and puts all of the step timing interval in one place. That will also help with the interleaving.

…R