Looking for help on driving multiple steppers from one Arduino.
NEMA17 motors; DM320T Drivers; Power supply: MeanWell 24v 14A; Arduino UNO r3
Description: trying to run multiple (2) stepper motors from one Arduino that stop at random positions. Need each motor to run CW, CCW, independently and simultaneously. In the attached code, motors independently move CW to random position, stop, but then continue CW. Problem is to get them to reverse direction to a random position.
Thanks.
// 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 DIR 2, 6
#define STEP 3, 7
#define MOTOR_STEPS 200
AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 3);
AccelStepper stepper2(AccelStepper::FULL2WIRE, 6, 7);
int step1dir = 0;
int step2dir = 0;
void setup() {
Serial.begin(9600);
stepper1.setMaxSpeed(1500.0);
stepper1.setAcceleration(1500.0);
stepper1.move(0);
stepper1.run();
stepper2.setMaxSpeed(1500.0);
stepper2.setAcceleration(1500.0);
stepper2.move(0);
stepper2.run();
}
void loop() {
//StepperMotor1:
if (step1dir == 0){
if (stepper1.distanceToGo() == 0){
stepper1.move(random(1000, 12000)); //CW
}
}
if(stepper1.distanceToGo() == 0 && step1dir == 1) {
step1dir = 0;667;
}
if(stepper1.distanceToGo() == 0 && step1dir == 0) {
step1dir = 1;
}
if (step1dir == 1){
if (stepper1.distanceToGo() == 0){
stepper1.move(-random(500, 6000)); //CCW
}
}
//StepperMotor2:
if (step2dir == 0){
if (stepper2.distanceToGo() == 0){
stepper2.move(random(1000, 12000)); //CW
}
}
if(stepper2.distanceToGo() == 0 && step2dir == 1) {
step1dir = 0;
}
if(stepper2.distanceToGo() == 0 && step2dir == 0) {
step1dir = 1;
}
if (step2dir == 1){
if (stepper2.distanceToGo() == 0){
stepper2.move(-random(500, 6000)); //CCW
}
}
stepper1.run();
stepper2.run();
}