Code structure for a Robot using Stepper motors

The actual problem: How to run blocking code to drive the motors and still have a function check for new serial input.

Some quick background:

I am driving 4 Nema24 stepper motors with a Mega. They use the M542T drivers (How and where to put in acceleration/deceleration? - Programming Questions - Arduino Forum)

Unless I am missing something right under my nose. The AccelStepper library does not work well with this Motor/Driver combo. The stepper library seems to be very buggy as well. This code snippet has worked for me 100% of the time however:

//Initialize pins for Motors
void initMotors(){
  // Motor 1
  pinMode(6, OUTPUT); //direction pin
  pinMode(7, OUTPUT); //step pin
  digitalWrite(6, LOW); //LOW = FORWARD, HIGH = REVERSE
  digitalWrite(7, LOW); //LOW = "OFF", HIGH = "ON"
  Serial.println("Motor 1 initialized");

  //Motor 2
  pinMode(8, OUTPUT); //direction pin
  pinMode(9, OUTPUT); //step pin
  digitalWrite(8, HIGH); //HIGH = FORWARD, LOW = REVERSE
  digitalWrite(9, LOW); //LOW = "OFF", HIGH = "ON"
  Serial.println("Motor 2 initialized");

  //Motor 3
  pinMode(22, OUTPUT); //direction pin
  pinMode(24, OUTPUT); //step pin
  digitalWrite(22, LOW); //LOW = FORWARD, HIGH = REVERSE
  digitalWrite(24, LOW); //LOW = "OFF", HIGH = "ON"
  Serial.println("Motor 3 initialized");

  //Motor 4
  pinMode(51, OUTPUT); //direction pin
  pinMode(53, OUTPUT); //step pin
  digitalWrite(51, HIGH); //HIGH = FORWARD, LOW = REVERSE
  digitalWrite(53, LOW); //LOW = "OFF", HIGH = "ON"
  Serial.println("Motor 4 initialized");

  Serial.println("Propulstion system is ready");
}


void dir_one(){

  if(alterDirection == true){
    //Move Forward (Direction #1, or ninety degrees)
    dirMatrix[0][0] = 1;
    dirMatrix[1][0] = 1;
    dirMatrix[2][0] = 1;
    dirMatrix[3][0] = 1;
   
    setDirection();
    alterDirection = false;
  }
  
  //All motors forward
  Serial.println("Activating motors 1-4 forward");

  if(!alterDirection){
    digitalWrite(7, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(24, HIGH);
    digitalWrite(53, HIGH);
    delayMicroseconds(speed);
    digitalWrite(7, LOW);
    digitalWrite(9, LOW);
    digitalWrite(24, LOW);
    digitalWrite(53, LOW);
    delayMicroseconds(speed);
  }
}

I am using Mechanum wheels so the robot can move in 1 of 8 directions without changing the orientation of it's forward camera. I have 8 functions, one for each direction. The example code above shows only the forward code - or direction #1. Not as elegant as I would like, but I am relatively new to the arduino and the limits of the micro-controller are providing interesting challenges (no multi-threading!)

To maintain smooth wheel movement I run the digitalWrite command in a loop with a speed setting for the delay in microseconds.

The arduino will receive serial input from a Nvidia TX2 doing the navigation processing.

The serial input is a single character 0-9 (9 = stop).

The blocking code of driving the wheels is not allowing the Loop() function to check for new serial input.

This design intends to put the robot in motion in one of the eight directions and not stop UNTIL it receives a command to stop or change direction. One solution would be to move in small increments and stop but that would lead to very choppy movement.

I am looking for ideas on to help me structure the code so the wheels can move continuously in a given direction until new serial input arrives to stop the movement or change direction.

Thanks in Advance!

How to run blocking code to drive the motors and still have a function check for new serial input.

You can’t. There is NO reason to have blocking code to driver the steppers. On any pass through loop(), the stepper either is, or is not, at the required position. If it isn’t, it either is, or is not, time to step again. If it is, step.

Thank you for the quick feedback.

I had a difficult time getting smooth wheel movement by stepping from code that originates in the Loop function. Do you have an example snippet perhaps?

Thanks

Do you have an example snippet perhaps?

   stepper.step(1);

Why are you not using the Stepper library?

I need to drive multiple motors simultaneously - anywhere from 2-4. I tried the AccelStepper library first and the movement was erratic, "jerky" and unacceptable. I also tried to use the Stepper Library and I don't know if it the motor/driver combo I am using but I had the same results as the AccelStepper library.

I will revisit the stepper library and see if i can make that work.

Thanks again for the input.

There are three stepper libraries - Stepper, AccelStepper, and MultiStepper. Stepper only allows one stepper at a time to move. AccelStepper allows multiple steppers to move at a time, but with no coordination. MultiStepper allows multiple steppers to move at the same time, in a coordinated fashion.

If you want to write your own stepping code without any library and without blocking then look at the second example in this Simple Stepper Code

And when posting code please post a complete program rather than snippets.

...R
Stepper Motor Basics