Controlling Stepper 5 Motors over Serial

Hey, i have a project where i want to controll 5 Stepper Motors with 5 values send from another Arduino. I managed to get the values parsed and they can also controll the steppers with the accel library and and the runToNewPosition command. But this runs only one motor until the desired position and does nothing else at that time. since i want up to 2000 steps per second per motor it seems i cant check for new values at the same time driving the motors. Is there a way arround? like checking if the values send have changed? This is my code for receiving data which does not work when i try to run one step every loop. :

void loop() {//if we have serial data
  if (Serial2.available() >0) {

    move_finished=0;
    
    // read one more data into the buffer
    dataBuffer[dataBufferIndex] = Serial2.read();
    // if no message start yet
    if (!messageStarted) {
      boolean dataBufferIndexIndrease = true;
      // if message is at lesat 2 byte long
      if (dataBufferIndex > 1) {
        if (dataBuffer[dataBufferIndex - 1] == 253 && dataBuffer[dataBufferIndex] == 255) {
          // we set the flag
          messageStarted = true;
          // reset the index
          dataBufferIndex = 0;
          dataBufferIndexIndrease = false;
        } //end if
      } //end if (dataBufferIndex > 1)

      // check if we need to increase the index
      if (dataBufferIndexIndrease)
        dataBufferIndex++;
      //  Serial.print(dataBufferIndex);
    }
    // if message is started
    else
    {
      // if we got to the last byte of the message
      if (dataBufferIndex == 6)
      {
        // check the checksum
        if ((byte) (dataBuffer[0] +  dataBuffer[1] +  dataBuffer[2] +  dataBuffer[3] +  dataBuffer[4]) == dataBuffer[5]) {
 
         int MOTORX = dataBuffer[0] ;
         MOTORX = map(MOTORX, 0, 255, 0, stepsX);
         int MOTORY = dataBuffer[1] ;
         MOTORY = map(MOTORY, 0, 255, 0, stepsY);
         int MOTORZ = dataBuffer[2] ;
         MOTORZ = map(MOTORZ, 0, 255, 0, stepsZ);
         int MOTORE = dataBuffer[3] ;
         MOTORE = map(MOTORE, 0, 255, 0, stepsE);
         int MOTORQ = dataBuffer[4] ;
         MOTORQ = map(MOTORQ, 0, 255, 0, stepsQ); 

        stepperx.runToNewPosition(MOTORX);


        } else {
          Serial.println("Error reading data - fail on check sum");
           stepperx.moveTo(20000);
        stepperx.run(); }
        } //end if

        // reset the flag
        messageStarted = false;
      } //end if (dataBufferIndex == 3)

      // increase the buffer index
      dataBufferIndex++;
    } //end if (!messageStarted)
  } //end if Serial1.available()

Thanks for any tips. Isnt there a example code out there where i can look into how to do this? I search for weeks now and do not find any anser to this. Greetings

mateoman:
since i want up to 2000 steps per second per motor it seems i cant check for new values at the same time driving the motors.

If you want to do 2000 steps per second for 5 motors that would be 10,000 steps per second and I don't know that the AccleStepper library can manage that.

To have several motors work at the same time use run() rather than runToNewPosition().

As far as getting serial data is concerned, you need to provide more information about what the whole machine is intended to do. Perhaps there will be periods when few or no steps are needed?

What is sending the serial data?

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.

“since i want up to 2000 steps per second per motor i”

I don’t know much about stepper motors, but do you have a stepper motor that will accurately move at that rate? Just seems a little optimistic.

@Zoomkat: I dont run the motor on full steps, what i ment was microsteps. Maybe it would be a good idea to run them on full steps since the dont need to be hyperprecise. With microsteps, 2000steps/s are not a lot.

@Robin2: I have a Arduino uno with a DMX shield which sends the data as bytes to an arduino mega. DMX signals are values from 0 to 255 so bytes are perfect here. then i want to receive the data on the mega, parse it, map it to the min/max range of the motors and let the values controll the motors. What i build is a robotic like lightfixture which should react as fast as possible. Most of the time there wont drive all motors at the same time. But i still need to check while motors move if new values come and a second motor should start moving. I even saw cases where people did exactly that with arduinos but without explanation. When i use moveTo() and the run() command the motors dont move at all and just rattle. Greets

mateoman:
What i build is a robotic like lightfixture which should react as fast as possible. Most of the time there wont drive all motors at the same time. But i still need to check while motors move if new values come and a second motor should start moving.

That does not answer my question in Reply #1 in a way that enables me to visulaise how the thing works and when, and for what period, each motor might move. It is only by knowing the fine details that a solution can be suggested.

When i use moveTo() and the run() command the motors dont move at all and just rattle.

Without seeing the complete program I can't comment. The snippet you have posted seems to use the blocking runToNewPosition() function.

The way you appear to be receiving serial data does not seem at all robust. How will the Arduino know when the data starts or finishes? Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

More information about how and when the data is produced, and some examples of actual messages would also help.

Your program will be much easier to develop and debug and maintain if you break the code into separate short single-purpose functions rather than putting it all in loop(). Have a look at Planning and Implementing a Program

...R