AccelStepper issue with 2nd Motor

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);
}

Even experienced people will often start wit a hardware or library example sketch and then build on that. There should be an example in the library that will get you up and runing quickly.

I do not know what that is. I think you should look at the examples.

Does your motor driver use 3v3 i/o?

Thanks - I’ve just gone back to the multiple steppers example, modified the pin numbers and get the same result. Only pins 20 and 21 are operating on the second motor. Maybe it doesn’t like the higher pin nos… I’ll try rewiring to the pins given in the example so I can run it unchanged.

Me neither :grinning_face: - using the phrase from the comment in one of the examples. I mean the two lines of code that create the stepper motor instances / drivers.

AccelStepper stepper1(AccelStepper::FULL4WIRE, 8, 9, 10, 11);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 21, 20, 19, 18);

I don’t think it’s the drive voltage - I can see the pin states from the LEDs I have on them, and in one mode they are fine but not in the other. I’m using L298’s which have a high state of 2.3V so 3.3 is enough.

OK - so having rewired the second motor to pins 2-5 it works as expected. Pins 18 and 19 are dual function with TX1 and RX1, maybe they are conflicting with the motor drive - but still have no idea why they work sometimes and not others. Will do some more investigating…

I can’t find anything saying pins 18 and 19 should be avoided, but everything seems to work if I do, so I will! I also had trouble using Serial.print with AccelStepper (I realise it is blocking) - the serial monitor was showing garbage every time the system booted and none of my Serial.print messages came though. Having changed the pins this also now works, which adds to my suspicion that the serial function is conflicting somehow, even though its on Serial0 not Serial1. Thanks all for your suggestions.

The pinouts show 18 and 19 are also PWM pins. Guessing... Maybe the duty cycle was set too short or too long.