Stepper motors move slower when new code added

Hi, I'm making a project which moves 3 stepper motors with an esp32 microcontroller (like arduino but smaller) through wireless communication called ESP-NOW. (Everything there works fine).

When a stepper motor is isolated in the code it runs great and at the correct speed, but when more code is added (the code for the other steppers) the motor runs slower than usual. I think this is because the microcontroller is waiting for the data being sent over for the other stepper motors causing a delay in pulses and slowing the motor.

My code is as follows:

//For ESP32 microcontroller

#include <esp_now.h> //ESP-NOW
#include <WiFi.h> //ESP-NOW
#include <ESP32Servo.h> //Servo control
#include <AccelStepper.h> //Stepper control (easier than default anyway)

int dataLed = 19;

Servo servo1;
Servo gripperServo;

//AccelStepper topActuator(1, 17, 18);   // (Type:driver(1 is default driver), STEP, DIR)
AccelStepper midActuator(1, 17, 18);   // (Type:driver(1 is default driver), STEP, DIR)
AccelStepper baseActuator(1, 13, 14);   // (Type:driver(1 is default driver), STEP, DIR)

float baseActuatorSpeed;
float midActuatorSpeed;
int servo1Angle;

int gripperServoAngle;

//Structure data being recieved
typedef struct struct_message {
    int yData;
    int xData;
    int flexData;
} struct_message;

struct_message dataSent; //Declare data recieved

//Call data recieved
void data_receive(const uint8_t * mac, const uint8_t *incomingData, int len) {
   memcpy(&dataSent, incomingData, sizeof(dataSent));
   Serial.print("Bytes received: ");
   Serial.println(len);
   Serial.print("X-Rotation: ");
   Serial.println(dataSent.xData);
   Serial.print("Y-Rotation: ");
   Serial.println(dataSent.yData);
   Serial.print("Flex data: ");
   Serial.println(dataSent.flexData);
   }

 void setup() {
  Serial.begin(115200); //Begin serial communication

  WiFi.mode(WIFI_STA); //Initialize WiFi for ESP-NOW
  esp_now_init();

  servo1.attach(21);
  gripperServo.attach(22);

  //Check ESP-NOW initilization (check for error with sending data)
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    digitalWrite(dataLed, LOW);
    return;
  }

  else {
    digitalWrite(dataLed, HIGH);
  }

  baseActuator.setMaxSpeed(1000); //Max speed for stepper
  midActuator.setMaxSpeed(1000);
  esp_now_register_recv_cb(data_receive); //Recieve data

}
 
void loop() {
  
  //Map incoming values
  midActuatorSpeed = map(dataSent.yData, 0, 180, -800, 800);
  gripperServoAngle = dataSent.flexData; //Set gripper servo to flex data
  baseActuatorSpeed = map(dataSent.xData, 0, 180, -800, 800); //Map x rotation values to speed values
  

  //Control gripper
  gripperLoop();
  
  //Control base stepper
  baseActuatorLoop();
  
  //Control mid stepper
  midActuatorLoop();
  
  //Control top stepper
  
  midActuator.runSpeed(); //Run stepper
  baseActuator.runSpeed(); //Run stepper

  //Unable to add delay because it will mess with stepper pulses :(
}

void midActuatorLoop() {
  midActuator.setSpeed(midActuatorSpeed); //Send the speed values to the stepper 
  Serial.print("Mid Actuator Speed: ");
  Serial.println(midActuatorSpeed);
}

void baseActuatorLoop() {
  baseActuator.setSpeed(baseActuatorSpeed); //Send the speed values to the stepper 
  Serial.print("Base Actuator Speed: ");
  Serial.println(baseActuatorSpeed);
}

void gripperLoop() {
  if (gripperServoAngle > 70) { //Grip completely if close
    gripperServoAngle = 90;
  }
  Serial.print("flexData: ");
  Serial.println(90 - gripperServoAngle); //Make 0 default
  gripperServo.write(gripperServoAngle); //Write servo angle to gripper
}

Is there any way I can stop the motor from spinning slower with the code for the other stepper motors integrated?

Thank you for your help!

Since you have debugged your program, get rid of all the serial.print().

2 Likes

All that printing takes time, and it is blocking.

1 Like

It works now, I'm dumb lol. Thank you so much!

It works now, thank you!

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