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
}
}
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:
Supports acceleration and deceleration
Supports multiple simultaneous steppers, with independent concurrent stepping on each stepper
Most API functions never delay() or block (unless otherwise stated)
Supports 2, 3 and 4 wire steppers, plus 3 and 4 wire half steppers.
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.
// 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.