serial arduino - independent steppers

Hello everyone,

During working on my project, I faced a problem with controlling stepper motors...

A brief introduction

Arduino mega is receiving data (0 or 1) from Kinect via processing for activation each of eight stepper motors. When stepper gets 1, it has to rotate to a certain number of steps(15000), and then, if 1 changes to 0 - it turns to an initial position.

In this way, all steppers should be able to rotate simultaneously, but independently. And this is my stumbling block. I need to have a non-blocking code, but at the same time steppers should reach 15000 steps once they get 1.

Message from Processing has eight integers and consist of seven 0 and one 1.

I have tried AccelStepper library, but did not succeed. Whether motors are moving slowly and unevenly or continuously and blocking the code.

Steppers - 42BYGHW609
Drivers - a4988

If you have any ideas, I would appreciate any help

Here is my arduino code:

#include <AccelStepper.h>

AccelStepper stepper1(1,22,23);
AccelStepper stepper2(1,24,25);
AccelStepper stepper3(1,26,27);
AccelStepper stepper4(1,28,29);
AccelStepper stepper5(1,30,31);
AccelStepper stepper6(1,32,33);
AccelStepper stepper7(1,34,35);
AccelStepper stepper8(1,36,37);


char HEADER = 'H';
char A_TAG = 'M';
int TOTAL_BYTES = 8; // the total bytes in a message

//data variables

int oldA,oldB,oldC,oldD,oldE,oldF,oldG,oldH,oldK = 0; //Storing previous value
int a,b,c,d,e,f,g,h,k = 0;

void setup(){

  stepper1.setMaxSpeed(500);
  stepper2.setMaxSpeed(500);
  stepper3.setMaxSpeed(500);
  stepper4.setMaxSpeed(500);
  stepper5.setMaxSpeed(500);
  stepper6.setMaxSpeed(500);
  stepper7.setMaxSpeed(500);
  stepper8.setMaxSpeed(500);

   
  Serial.begin(9600);

}

void loop(){
  //Reading data from Kinect
char tag;
  if ( Serial.available() >= TOTAL_BYTES)
  {
    if( Serial.read() == HEADER)
    {
      tag = Serial.read();
      if(tag == A_TAG)
      {
        //Collect integers
        a = Serial.read();
        b = Serial.read();
        c = Serial.read();
        d = Serial.read();
        e = Serial.read();
        f = Serial.read();
        g = Serial.read();
        h = Serial.read();
        k = Serial.read();
        
        Serial.print("Received integers | a:");
        Serial.print(a);
        Serial.print(", b:");
        Serial.print(b);
        Serial.print(", c:");
        Serial.print(c);
        Serial.print(", d:");
        Serial.print(d);
        Serial.print(", e:");
        Serial.print(e);
        Serial.print(", f:");
        Serial.print(f);
        Serial.print(", g:");
        Serial.print(g);
        Serial.print(", h:");
        Serial.print(h);
        Serial.print(", k:");
        Serial.println(k);
                   
      } 
      
      } else {
        Serial.print("got message with unknown tag ");
        Serial.write(tag);
      }
    }

  
if(a > oldA){
  stepper1.moveTo(15000);
  stepper1.run();
 }
 if(a < oldA){
  stepper1.moveTo(0);
  stepper1.run();
 }
if(b > oldB){
  stepper2.moveTo(15000);
  stepper2.run();
 }
if(b < oldB){
  stepper2.moveTo(0);
  stepper2.run();
 }
if(c > oldC){
  stepper3.moveTo(15000);
  stepper3.run();
 }
if(c < oldC){
  stepper3.moveTo(0);
  stepper3.run();
 }
if(d > oldD){
  stepper4.moveTo(15000);
  stepper4.run();
 }
if(d < oldD){
  stepper4.moveTo(0);
  stepper4.run(); 
 }
if(f > oldF){
   stepper5.moveTo(15000);
   stepper5.run();
  }
if(f < oldF){
   stepper5.moveTo(0);
   stepper5.run(); 
  }
if(g > oldG){
   stepper6.moveTo(15000);
   stepper6.run();
  }
if(g < oldG){
   stepper6.moveTo(0);
   stepper6.run(); 
  }
if(h > oldH){
   stepper7.moveTo(15000);
   stepper7.run();
  }
if(h < oldH){
   stepper7.moveTo(0);
   stepper7.run(); 
  }
if(k > oldK){
   stepper8.moveTo(15000);
   stepper8.run();
  }
if(k < oldK){
   stepper8.moveTo(0);
   stepper8.run(); 
  }

//Updating previous value
oldA = a;  
oldB = b;
oldC = c;
oldD = d;
oldF = f;
oldG = g;
oldH = h;
oldK = k;

}

Not the greatest Serial code, but not the worst either. But it does have an issue to resolve...

Your code seems to read a header "H" then a tag "M" and then 9 bytes after that. But you only checked if there were TOTAL_BYTES in the buffer. That constant is 8. So you are trying to read 11 characters from a buffer that only contains 8.

if(a > oldA){

stepper1.moveTo(15000);
  stepper1.run();
}
if(a < oldA){
  stepper1.moveTo(0);
  stepper1.run();
}

Don't put the .run() methods inside these if() statements. Put all the .run() methods near the bottom. You must call .run() on every stepper on every loop. It doesn't matter if the stepper has already reached its destination, in that case .run() will not move the stepper.

AccelStepper stepper1(1,22,23);
AccelStepper stepper2(1,24,25);
AccelStepper stepper3(1,26,27);
AccelStepper stepper4(1,28,29);
AccelStepper stepper5(1,30,31);
AccelStepper stepper6(1,32,33);
AccelStepper stepper7(1,34,35);
AccelStepper stepper8(1,36,37);

When you start numbering variables, arrays become an obvious choice. Your code would be a LOT simpler if you had an array of steppers.

int oldA,oldB,oldC,oldD,oldE,oldF,oldG,oldH,oldK = 0; //Storing previous value
int a,b,c,d,e,f,g,h,k = 0;

Are you sure you don't need long, or long long, to hold 0 or 1?

You are sending 8 bits of information, which could be sent in one byte, with no need for header or footer.