Hello,
My aim is to use two stepper motor at the same time, but the total steps for both motors will differ during the experiment phase.
Arduino: Mega2560
Motor: 48steps/rev, 6v Rated Voltage, 250mA Current per winding, 7.5deg/step
I tried two ways:
- Adafruit Motor Shield V2
2)A4988 Motor driver(similar to polulu)
The problem with the first approach with shield is, for max steps of 1200(one requirement) both motors take 8/9sec to turn.
#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS(0x60); // Default address, no jumpers
// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(48, 1);
Adafruit_StepperMotor *myStepper2 = AFMS.getStepper(48, 2);
// wrappers for the first motor!
void forwardstep1() {
myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {
myStepper2->onestep(FORWARD, SINGLE);
}
void backwardstep2() {
myStepper2->onestep(BACKWARD, SINGLE);
}
// Now we'll wrap the 2 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);
{
AFMS.begin(); // Start the top shield
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(300.0);
//stepper1.setSpeed(300);
stepper1.moveTo(1200);
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(300.0);
//stepper2.setSpeed(300);
stepper2.moveTo(600);
}
void loop()
{
if ((stepper1.distanceToGo() != 0)||(stepper2.distanceToGo() != 0))
{
stepper1.run();
stepper2.run();
}
else
{
stepper1.stop();
stepper2.stop();
}
}
}
The problem with the second approach with driver is, I could not find out the way for sopping the motors after certain steps. Say after 1000 steps stop the motor perform experiments and again turn a few steps. // Define pin connections & motor's steps per revolution
//with driver
const int dirPin1 = 36;
const int stepPin1 = 2;
const int dirPin2 = 37;
const int stepPin2 = 3;
const int enPin1 = 38; //enable pin of driver1
const int enPin2 = 39; //enable pin of driver2
const int stepsMotor1 = 1200;
const int stepsMotor2 = 900;
void setup()
{
// Declare pins as Outputs
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(enPin1, OUTPUT);
pinMode(enPin2, OUTPUT);
}
void loop()
{
// Set motor direction clockwise/Anticlockwise
digitalWrite(enPin1, LOW); //enable the driver
digitalWrite(enPin2, LOW);
digitalWrite(dirPin1, HIGH);
digitalWrite(dirPin1, LOW);
for(int x=0; x<stepsMotor1; x++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(1200);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(1200);
}
delay(1000);
digitalWrite(enPin1, HIGH); //disable the driver
digitalWrite(enPin2, HIGH);
}