How can I move two motor at the same time?

I’m making a set up with two stepper motors. But I don’t know the code that can make them move at the same time but in different direction eg. Clockwise & anti-clockwise. Now stepper motor B will move after the motor A complete its rotation.


#include <Wire.h>
#include <VL53L0X.h>
#include <Stepper.h>

VL53L0X mySensor;  // Create a named VL53L0X object
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution of your stepper motor
Stepper myStepper1(stepsPerRevolution, 8, 10, 9, 11); // initialize the first stepper motor
Stepper myStepper2(stepsPerRevolution, 4, 6, 5, 7); // initialize the second stepper motor

void setup()
{
   Serial.begin(115200);
   Wire.begin();
   mySensor.init();  // Use the named object to initialize the sensor
   mySensor.setTimeout(500);  // Use the named object to set the timeout for the sensor
   myStepper1.setSpeed(5); // ********* 60RPM way too fast
   myStepper2.setSpeed(5); // set the speed for the second motor
   // ********* set the mode 100 measurements per second
   mySensor.startContinuous(100); 
}

void loop()
{
   uint16_t distance = mySensor.readRangeSingleMillimeters();  // Use the named object to get the distance measurement
   Serial.print("distance = ");  // ********** added
   Serial.println(distance); // ********** added
   if (distance <= 80)
   {
      myStepper1.step(stepsPerRevolution); // rotate the first stepper motor once
      myStepper2.step(stepsPerRevolution); // rotate the second stepper motor once
      delay(3000); // wait for 3 second before taking another measurement
   }
}

I not sure it can be done using the Stepper library.

I would use the Accelstepper library.

The standard Arduino IDE includes the Stepper library (Stepper - Arduino Reference) for stepper motors. It is perfectly adequate for simple, single motor applications.

AccelStepper significantly improves on the standard Arduino Stepper library in several ways:

Another library for steppers is the MobaTools stepper library. I believe that it is easier to learn and use than AccelStepper and has most of the same features. The MobaTools library is available through the IDE library manager.

https://www.airspayce.com/mikem/arduino/AccelStepper/MultipleSteppers_8pde-example.html

// MultiStepper.pde
// -*- mode: C++ -*-
//
// Shows how to multiple simultaneous steppers
// Runs one stepper forwards and backwards, accelerating and decelerating
// at the limits. Runs other steppers at the same time
//
// Copyright (C) 2009 Mike McCauley
// $Id: MultiStepper.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $
 
#include <AccelStepper.h>
 
// Define some steppers and the pins the will use
AccelStepper stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 7, 8, 9);
AccelStepper stepper3(AccelStepper::FULL2WIRE, 10, 11);
 
void setup()
{  
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(100.0);
    stepper1.moveTo(24);
    
    stepper2.setMaxSpeed(300.0);
    stepper2.setAcceleration(100.0);
    stepper2.moveTo(1000000);
    
    stepper3.setMaxSpeed(300.0);
    stepper3.setAcceleration(100.0);
    stepper3.moveTo(1000000); 
}
 
void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
        stepper1.moveTo(-stepper1.currentPosition());
    stepper1.run();
    stepper2.run();
    stepper3.run();
}

Here is a simple program to control 2 steppers over serial. Type a single letter command in serial monitor to command the motors to move forward (both CW), back (both CCW), right and left (one motor CW and the other CCW). It uses the MobaTools library.

#include <MobaTools.h>

MoToStepper rightStepper(2048, FULLSTEP );  
MoToStepper leftStepper(2048, FULLSTEP);

void setup()
{
   Serial.begin(115200);
   Serial.println("stepper control with serial\nB = reverse\nF = forward\nL = left\nR = right\nS = stop");
   rightStepper.attach( 8, 9, 10, 11 );
   leftStepper.attach( 4, 5, 6, 7 );
   rightStepper.setSpeed( 100 );  // desired RPM x 100.
   rightStepper.setRampLen(10);
   rightStepper.setZero();
   leftStepper.setSpeed( 100 );  // desired RPM x 100.
   leftStepper.setRampLen(10);
   leftStepper.setZero();
}

void loop()
{
   checkSerial();
}

void reverse()
{
   Serial.println(F("  going reverse"));
   rightStepper.setSpeed( 100 );
   leftStepper.setSpeed( 100 );
   rightStepper.rotate(-1);
   leftStepper.rotate(-1);
}

void forward()
{
   Serial.println(F("  going forward"));
   rightStepper.setSpeed( 100 );
   leftStepper.setSpeed( 100 );
   rightStepper.rotate(1);
   leftStepper.rotate(1);
}

void right()
{
   Serial.println(F("  right turn"));
   rightStepper.setSpeed( 100 );
   leftStepper.setSpeed( 100 );
   rightStepper.rotate(-1); 
   leftStepper.rotate(1);
}

void left()
{
   Serial.println(F("  left turn"));
   rightStepper.setSpeed( 100 );
   leftStepper.setSpeed( 100 );
   rightStepper.rotate(1);
   leftStepper.rotate(-1); 
}

void stopCar()
{
   Serial.println(F("  stop the car"));
   rightStepper.rotate(0);
   leftStepper.rotate(0);
}

void checkSerial()
{
   if (Serial.available())   // Witing for data incoming 
   {
      char tiltDirection = Serial.read();
      Serial.print(F("new direction  "));
      Serial.println(tiltDirection);
      if (tiltDirection == 'B')
      {
         Serial.println(" reverse");
         reverse();
      }
      else if (tiltDirection == 'F')
      {
         Serial.println("Forward");
         forward();
      }
      else if (tiltDirection == 'L')
      {
         Serial.println("Left");
         left();
      }
      else if (tiltDirection == 'R')
      {
         Serial.println("Right");
         right();
      }
      else if (tiltDirection == 'S')
      {
         Serial.println("Stop");
         stopCar();
      }
   }
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.