Hello all - I’ve volunteered to rebuild the control electronics for a model of the Chatley Heath semaphore tower but have been struggling for a couple of days trying to get the second stepper motor working.
In the code below if I put the creator code at the top, as per all the examples, only Stepper1 runs normally. Stepper2 only powers the first two (of 4) outputs (like a DC motor drive) and doesn’t half or full step. If I put the class creator code within the moveMotors function both motors run correctly (but they lose position between moves due to reinitialising the class).
I believe the hardware is working as both motors run correctly with the class creator in the function. I’ve manually coded enabling and disabling power, but have also tried using the built in enable / disable functions in AccelStepper with no difference. I have LEDs on the drive signals so can see it’s only driving two of the four outputs when programmed as below.
Any ideas what I’m doing wrong?
Using an Arduino Due (Professional Hardware Eng but amateur programmer).
#include <AccelStepper.h>
// change this to the number of steps on your motor
#define STEPS 400 //steps per revolution (halfstepping)
#define enable1 12
#define enable2 17
//Only Motor 1 runs correctly with class creators here
AccelStepper stepper1(AccelStepper::HALF4WIRE, 8, 9, 10, 11);
AccelStepper stepper2(AccelStepper::HALF4WIRE, 21, 20, 19, 18);
void setup() {
pinMode(14,INPUT_PULLUP); //Push button input
pinMode(enable1,OUTPUT); //Set up motor enable pins
digitalWrite(enable1,LOW);
pinMode(enable2,OUTPUT);
digitalWrite(enable2,LOW);
}
void loop() {
int key = 0;
key = digitalRead(14); //Read button state
if (key == 0 ) { //If button pressed
int stps = 150;
moveMotors(stps,stps);
delay(200);
moveMotors(-stps,-stps);
}
}
void moveMotors(int m1pos, int m2pos) {
//Motors run correctly with the class creator here, except for losing step between calls
//AccelStepper stepper1(AccelStepper::HALF4WIRE, 8, 9, 10, 11);
//AccelStepper stepper2(AccelStepper::HALF4WIRE, 21, 20, 19, 18);
stepper1.setMaxSpeed(100.0);
stepper1.setAcceleration(2000.0);
stepper2.setMaxSpeed(100.0);
stepper2.setAcceleration(2000.0);
digitalWrite(enable1,HIGH); //Enable the motor drive
stepper1.move(m1pos); //Set next paddle position
digitalWrite(enable2,HIGH); //Enable the motor drive
stepper2.move(m2pos); //Set next paddle position
do {
stepper1.run();
stepper2.run();
} while (stepper1.distanceToGo() != 0 || stepper2.distanceToGo() != 0);
digitalWrite(enable1,LOW);
digitalWrite(enable2,LOW);
}
