Stepper Motor Code Help Needed

Hello:
I want to create a script that will allow me to run two stepper motors forwards, backwards, control the speed of each motor and somehow set time/distance for each.
I am using a Mega 2560 R3 connected to two Adafruit TMC2209 Stepper Motor Driver Breakout Boards.
All the electronics work, and I can move the motors Forwards or Backwards & control the speed. It's the time/distance part that I am having trouble making work. Below is what I have so far.

Any help would be appreciated in solving this -

#include <Stepper.h>

// Define motor pins

// Motor One
const int directionPin_1 = 1; 
const int stepPin_1 = 2;

// Motor Two
const int directionPin_2 = 3; 
const int stepPin_2 = 4;

// Even thou the TMC2209 is wired to pin D1 you can use just 1 here.
// If you use pin A1 it must be designated as A1, and not just 1.

// Forwards or backwards
// Forwards = LOW
// Backwards = HIGH

// Motor One
int Direction_1 = LOW;
// Motor Two
int Direction_2 = LOW;

// Assuming a 200-step motor
const int stepsPerRevolution = 200; 

// Define motor speed
const int motorSpeed = 100; // RPM

// Microseconds (adjust for desired motor speed. The larger the number the slower the motor speed)

// Motor One
const int delay_time_1 = 200;
// Motor Two
const int delay_time_2 = 200; 

// Time-Distance moved. 200 Steps per revolution of motor

// Motor One
unsigned long duration_1 = 5000; // Run for 5 revolutions of the wheel
// Motor Two
unsigned long duration_2 = 5000; // Run for 5 revolutions of the wheel

bool motorRunning = false;

// Create a Stepper object

// Motor One
Stepper myStepper_1(stepsPerRevolution, stepPin_1, directionPin_1);
// Motor Two
Stepper myStepper_2(stepsPerRevolution, stepPin_2, directionPin_2);


void setup() {
  // Set pins as output
  
  // Motor One
  pinMode(stepPin_1, OUTPUT);
  pinMode(directionPin_1, OUTPUT);
  //
  pinMode(stepPin_2, OUTPUT);
  pinMode(directionPin_2, OUTPUT);
  
  // Set the motor speed (optional, but recommended)
  // Motor One
  myStepper_1.setSpeed(motorSpeed);
  // Motor Two
  myStepper_2.setSpeed(motorSpeed);

   // Start the motor
  motorRunning = true;
}

void loop() {
 if (motorRunning) {
  // Start time
    unsigned long startTime = millis();
    // Run motor for the duration
    while (millis() - startTime < duration_1) {

  // Move the motor one step
  
  // Motor One
  digitalWrite(directionPin_1, Direction_1); // Rotate Backwards or Forwards
  digitalWrite(stepPin_1, HIGH);
  delayMicroseconds(delay_time_1);
  digitalWrite(stepPin_1, LOW);
  delayMicroseconds(delay_time_1);

    // Motor Two
  digitalWrite(directionPin_2, Direction_2); // Rotate Backwards or Forwards
  digitalWrite(stepPin_2, HIGH);
  delayMicroseconds(delay_time_2);
  digitalWrite(stepPin_2, LOW);
  delayMicroseconds(delay_time_2);
  }
    motorRunning = false; // Reset flag
  }
  
}

Time I can understand, but motors moving a distance is beyond me. Are they in some type of vehicle?

Yes each motor is connected to a wheel.

not clear what the problem is

  • looks like your code should repeatedly step both motors for 5 secs without any delay between steps. i've found that stepping more frequently than once/msec is often too fast.

  • your code attempts to do this just once,

  • you're using pin which is used as the TX pinfor serial communication.arduino

  • appears that you're using a Stepper library but manually stepping the motors instead of using the library

rather thatn try to explain what you can do, please look this over

// simple stepper controller

struct Motor {
    const byte PinDir;
    const byte PinStep;
    int        posTarg;     // target position
    int        pos;         // current position
}
motor [] = {
    { 10, 11 },             // set values to your pins
    { 12, 13 },
};
const int Nmotor = sizeof(motor)/sizeof(Motor);

enum { For = LOW, Rev = HIGH };

// -------------------------------------
void
moveMotor (
    int motorIdx )
{
    if (motor [motorIdx].pos < motor [motorIdx].posTarg)  {
        digitalWrite (motor [motorIdx].PinDir, For);
        motor [motorIdx].pos++;
    }
    else if (motor [motorIdx].pos > motor [motorIdx].posTarg)  {
        digitalWrite (motor [motorIdx].PinDir, Rev);
        motor [motorIdx].pos--;
    }
    else                                    // at target position
        return;

    delayMicroseconds (10 );
    digitalWrite      (motor [motorIdx].PinStep, HIGH);
    delayMicroseconds (10 );
    digitalWrite      (motor [motorIdx].PinStep, LOW);

    // this may need to be commented out or deleted at full speed
    Serial.print ("moveMotor: ");
    Serial.print (motorIdx);
    Serial.print (", pos ");
    Serial.println (motor [motorIdx].pos);
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (115200);

    for (int n = 0; n < Nmotor; n++)  {
        pinMode  (motor [n].PinDir,  OUTPUT);
        pinMode  (motor [n].PinStep, OUTPUT);
    }

    Serial.println ("simple stepper controller");
}

// -----------------------------------------------------------------------------
const unsigned long UsecStep = 250000;      // set to 1000 for 1 msec
      unsigned long usec0;

void loop ()
{
    unsigned long usec = micros ();
    if (usec - usec0 >= UsecStep)  {
        usec0 += UsecStep;

        for (int n = 0; n < Nmotor; n++)
            moveMotor (n);
    }

    // read command from serial monitor
    if (Serial.available ())  {
        char buf [90];
        int  n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';                     // terminate string

        // translate cmd and set target position for each motor
        int pos;
        sscanf (buf, "%d", & pos);
        for (int n = 0; n < Nmotor; n++)
            motor [n].posTarg = pos;
    }
}

To: gcjr

I have tried to use your posted code as is, and just moved the wires over the D10, D11, D12 & D13 pins. Nothing happened. I Then moved the wires over to A10, A11, A12 & A13 again nothing happened. I even tried including the stepper library - nothing happened. Forgive my ignorance, but I am still very new to Arduino and was not aware of what pins to use or why.

Sorry for not explaining this - My orginal code does work but when I change the speed it throws off the distance/time the wheels move forward.

please post that code

Its the code at the top of this page

i guess i don't understand the problem.

if that code works. don't the motors run for 5 seconds?

Yes and no.

If I change the speed number the motors run for less distance/ time. I need to be able to set the time/distance, and still be able to change the speed. I hope this explains what I am encountering. I truly don't understand the issue causing it. I just know the end result

if you use the library there's probably a command that specifies a position, accumlated # of steps, and the command will step the motor at the specified speed until that position is reached.

but while your setting the speed using the library function, your code is stepping the motor without using the library funciton.

and because your stepping the motors too fast, they partially move a step but then the motor fields reverse and they get pulled back

i believe if you just put a 1 msec delay in you while loop that motors should move, try a 10 msec delay .... delay, not microsecondDelay