Steppermotor 2nd motor

Hello
I’m trying to make my second steppermotor work according to the code. I know that I’m doing something wrong but what is it? Here is my code:

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <AccelStepper.h>

#define SERVOMIN  125
#define SERVOMAX  575
#define SERVO_FREQ 60
#define motorInterfaceType 1
#define motorInterfaceType 2
#define dirPin1 5
#define stepPin1 4
#define dirPin2 6
#define stepPin2 7

int motor1pin1 = 8;
int motor1pin2 = 9;
int enableDriver1 = 10;
int enableDriver2 = 12;
int microSwitchMuratcan = 3;
int microSwitchCammy = 2;
uint8_t servonum = 0;

AccelStepper stepper1 = AccelStepper(motorInterfaceType, stepPin1, dirPin1);
AccelStepper stepper2 = AccelStepper(motorInterfaceType, stepPin2, dirPin2);
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);

void setup() {
  pinMode(enableDriver1, INPUT);
  pinMode(enableDriver2, INPUT);
  pinMode(microSwitchMuratcan, INPUT);
  pinMode(microSwitchCammy, INPUT);
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  stepper1.setMaxSpeed(800);
  stepper2.setMaxSpeed(800);
  pwm.begin();
  pwm.setPWMFreq(SERVO_FREQ);
}

void loop() { 
  if(digitalRead(microSwitchMuratcan) == HIGH)
  {
     digitalWrite(motor1pin1, LOW);
     digitalWrite(motor1pin2, LOW);
     delay(2000);
     pwm.setPWM(3, 0, 235);
     delay(2000);
    {
     digitalWrite(enableDriver1, LOW);
     digitalWrite(enableDriver2, HIGH);
     stepper1.setCurrentPosition(0);
     while(stepper1.currentPosition() != 400)
    {
     stepper1.setSpeed(800);
     stepper1.runSpeed();
    }
     delay(2000);
     digitalWrite(enableDriver1, HIGH);
     digitalWrite(enableDriver2, LOW);
     stepper2.setCurrentPosition(0);
     while(stepper2.currentPosition() != 400)
    {
     stepper2.setSpeed(800);
     stepper2.runSpeed();
    }   
     delay(2000);
     digitalWrite(enableDriver1, LOW);
     digitalWrite(enableDriver2, HIGH);
     stepper1.setCurrentPosition(0);
     while(stepper1.currentPosition() != -400)
    {
     stepper1.setSpeed(-800);
     stepper1.runSpeed();
    }
     delay(2000);
     pwm.setPWM(3, 0, 90);
     delay(1000);
     digitalWrite(motor1pin1, HIGH);
     digitalWrite(motor1pin2, LOW);
     delay(5000);
    }
 } else {
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(motor1pin2, LOW);
 }

Stepper2 is the problem here. It needs to turn 400 signals but it doesn’t.

Hello.

I am not very familiar with the AccelStepper library, but it looks like you are defining motorInterfaceType twice, with the second declaration leaving it set to 2. Since you are defining STEP and DIR pins, I suspect you should be using an interface type of 1 according to the library’s documentation.

If that does not work, I recommend simplifying your setup for now and removing everything except the stepper motors and drivers. It looks like the library has an example for multiple stepper motors, so you might try running that with as few modifications as possible (e.g. the interface type and step/dir pins being used).

If that does not work either, could you post the modified example you tried running as well as information about your setup and the hardware you are using? Also, could you post pictures of your setup that show all of your connections?

Brandon

Do you have to have some sort of a component between the drivers and digital pins of the Arduino. because I need to make them turn seperately. That means that they are in parallel. Or is just and Arduino and the drivers enough to code the steppermotors seperately. I looked it up on the internet but all it gives me are tutorials and wirings to make the steppermotors turn simultanously.

You generally should not need any special components between the Arduino and the drivers. The Arduino should be using different pins for each stepper motor driver (as defined by your code).

Could you answer the questions I posed previously and post the requested information? In particular, please post:

  • Details about all of the hardware you are using (i.e. which Arduino, stepper motor drivers, stepper motors, and power supply you are using).

  • Pictures of your entire setup that show how everything is connected.

  • The modified “MultipleSteppers” example program from the library that I suggested running.

Additionally, could you tell me what you set the VREF voltage to on each of the stepper drivers?

Brandon