Stepper Motor delaying other tasks for a long time

Hi i set up two ultrasonic sensors and one OLED display with a stepper motor. When i execute the program, the line 'stepperName.step(Steps);' delays the execution of other lines by a long time.
Here is the code:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Stepper.h>

#define OLED_RESET 4



Adafruit_SH1106 display(OLED_RESET);

int step_number;
int check;
int c;
int trig1 = 4;
int echo1 = 5;
int trig = 7;
int echo = 6;
int timeInMicro;
float distanceInCm;
int Steps = 2048;
Stepper stepperName = Stepper(Steps, 8, 10, 9, 11);
void setup() {

  
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();

  Serial.begin(9600);
  pinMode(7, OUTPUT);
  pinMode(6, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, INPUT);
  stepperName.setSpeed(5);
  Serial.println("hj");
  
}

void loop() {

  Serial.println("test1");
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 15);


  Serial.println("test2");
  digitalWrite(trig1, LOW);
  delayMicroseconds(2);
  digitalWrite(trig1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig1, LOW);
  Serial.println("test3");


  check = pulseIn(echo1, HIGH);
  c = check / 30 / 2;
  display.println(c);
  Serial.println("test4");

  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  timeInMicro = pulseIn(echo, HIGH);
  display.setCursor(0, 0);
  distanceInCm = (timeInMicro / 30 / 2) * 1.732;
  Serial.println(distanceInCm);

  display.println(distanceInCm);
  display.display();
  display.clearDisplay();
  stepperName.step(Steps);
}

void ea(){
  stepperName.step(Steps);
}

Hi @YYoethan64 ,

welcome to the arduino-forum.
well done posting your code as a code-section in your very first post.

You are describing the behaviour of your code. This is good. But this is only a part of the information that is needed to really give help.

"Here is the code"
and that's all is really short.

I will give you an answer that has the same shortness: "Use MobaTools"

That's all.
Now what do you think?
Does it help?
I am pretty sure it does not help.

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

Welcome to the forum

When the code executes this line

    stepperName.step(Steps);

it does exactly what you tell it to, ie it executes a number of steps. During the time that it is doing this nothing else can happen, hence the delay in execution of other code

One way to fix this would be to only execute a single step each time that the code gets to that point and to keep a note of how many times a single step has been executed and stopping when the required number of steps has been executed.

If you did this then the rest of the code will have a chance to run between steps. Note, however, that any delays in the other code will slow down the motor

Implementing this solution using the stepper library would be a good programming exercise for you.

There are other libraries that could do what you want without blocking the running of other code, including this one

Its author is active on the forum if you need help

looks like the arg to setSpeed is the # of revolutions / min.
trying increase the that value from 5

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.