Stepper motors stutter while running

Hi
This is my first question here, so I hope Im posting into the right forum

I'm using an Arduino UNO with two 28-BYJ + ULN2003 stepper motors attached (and a HC-08 BLE shield to control them). To drive the motors I'm using the accelStepper library.

In short: with computer I send some values over Bluetooth (basically 1,2 or 3) and depending on the value the motors perform a different movement moving in parallel.

When the distanceToGo is 0 for both the motors I write an "idle state" value on the bluetooth, tth computer detects this change and send another value over Bluetooth, and so on.

The motors are powered with their own 4x1.25V AA batteries,
Arduino and the HC-08 are powered via USB

The problem: in a random way most of the times one or both the motors are stuttering and they stop during the movement for a while but they are actually keeping the information about the distance covered because I always receive back the idle value (255)

You can see the issue on this video - Here the value sent were
[1,2,1,3]

Here's my sketch:

#include <AccelStepper.h>
#include <SoftwareSerial.h>


#define FULLSTEP 4
#define HALFSTEP 8

#define M1_P1 8   // 28BYJ48 pin 1
#define M1_P2 9   // 28BYJ48 pin 2
#define M1_P3 10  // 28BYJ48 pin 3
#define M1_P4 11  // 28BYJ48 pin 4

#define M2_P1 4   // 28BYJ48 pin 1
#define M2_P2 5   // 28BYJ48 pin 2
#define M2_P3 6   // 28BYJ48 pin 3
#define M2_P4 7   // 28BYJ48 pin 4

int spd;
int code = 0;
int distance = 4096;


const int SPEED_CONTROL = A0;

// Define two motor objects
// The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper s1(FULLSTEP, M1_P1, M1_P3, M1_P2, M1_P4);
AccelStepper s2(FULLSTEP, M2_P1, M2_P3, M2_P2, M2_P4);
SoftwareSerial ble(2, 3); // rx, tx

void setupMotors() {
    s1.setMaxSpeed(1000.0);
    s1.setAcceleration(800.0);
    s1.move(1);
 
    s2.setMaxSpeed(1000.0);
    s2.setAcceleration(800.0);
    s2.move(1);

    s1.setCurrentPosition(0); 
    s2.setCurrentPosition(0);

    s1.setSpeed(800);
    s2.setSpeed(800);
}

void setup() {
    ble.begin(9600);     
    setupMotors();
}

void loop() {
    
    s1.run();
    s2.run();

    /* read bluetooth only if the previous movement completed */
    if (code == 0 && ble.available()) {
        code = (int)ble.read();
    }
 
    
    /* motors are running and the distance has been reached from both the motors? */
    if (code == -1 && s1.distanceToGo() == 0 && s2.distanceToGo() == 0) {
        code = 0;         // motors have completed the movement 
        ble.write(255);   // communicate idle state
    }
      

    if (code == 1) {
       code = -1;
       s1.move(distance); 
       s2.move(distance); 
    }
    
    if (code == 2) {
       code = -1;
       s1.move(-distance); 
       s2.move(distance); 
    }
    
    if (code == 3) {
       code = -1;
       s1.move(distance); 
       s2.move(-distance); 
    }
 
}

Any clues? There are no delays(), run() is always executed in the loop and I have no idea of what is going on.

Thank you

-f.

Your video link does not work for me. Can you post it on YouTube?

Your mechanism for receiving serial data is not robust. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

In a small CNC program that I have written the Arduino asks for the next message BEFORE it starts implementing the current move and that way the new data is ready to use as soon as the move is finished. Of course that would not be appropriate if the operator needs to make a decision after a move finishes.

...R

Here the youtube link : - YouTube

As described before, in this video you can see 4 values sent over bluetooth: 1, 2, 1, 3
Each value after the first is sent after I received the value 255.

I don't think this specific issue is related to how I read the value, since I don't send any new values until a movement has not completed (and I can't see how should I modify the code)

Thank you

9600 baud is slow, this will cause the software serial to block for noticable amounts of time.

Apparently the issue was caused for the excessive speed of the motors

I set the speed and the acceleration to 200 instead of 800 and it seems to work.