Controling two steppers with two rotary encoders

Hello I try to control two bipolar stepper motors with two rotary encoders. Encoder 1 to motor 1 and Encoder 2 to motor 2. I have this code:

#include <AccelStepper.h>

const int stepsPerRevolution = 200;  // change this to match the number of steps per revolution for your motor

// create instances of the AccelStepper class for each motor
AccelStepper motor1(1, 8, 10);
AccelStepper motor2(1, 9, 11);

// variables to store the current position of each rotary encoder
int encoder1Pos = 0;
int encoder2Pos = 0;

void setup() {
  // set the speed and acceleration of the motors
  motor1.setMaxSpeed(60);
  motor1.setAcceleration(100);
  motor2.setMaxSpeed(60);
  motor2.setAcceleration(100);

  // set the pin modes for the rotary encoder pins
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
}

void loop() {
  // read the current position of each rotary encoder
  int encoder1NewPos = readEncoder(2, 3);
  int encoder2NewPos = readEncoder(4, 5);

  // calculate the number of steps to move each motor
  int steps1 = encoder1NewPos - encoder1Pos;
  int steps2 = encoder2NewPos - encoder2Pos;

  // move the motors by the calculated number of steps
  motor1.move(steps1);
  motor2.move(steps2);

  // update the current position of the encoders
  encoder1Pos = encoder1NewPos;
  encoder2Pos = encoder2NewPos;

  // run the motors
  motor1.run();
  motor2.run();
}

// function to read the position of a rotary encoder
int readEncoder(int pinA, int pinB) {
  static int encoderPos = 0;
  static int lastEncA = LOW;
  static int lastEncB = LOW;

  int encA = digitalRead(pinA);
  int encB = digitalRead(pinB);

  if (encA != lastEncA) {
    if (encB != encA) {
      encoderPos++;
    } else {
      encoderPos--;
    }
  }

  lastEncA = encA;
  lastEncB = encB;

  return encoderPos;
}

the problem is that when I turn the encoder 1 the motor 1 rotates as i want but motor 2 also rotates but always in an negative virection no matter how i turn the knob. The same is for encoder 2.

Please help me

confusing.

using one encoder, motor 1 can be moved some # of steps and motor 2 -# of steps.

but what do you mean same for encoder 2? which encoder value do you want to use to determine the # of steps? or is it the greater of the 2?

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

Hello, I mean when I turn knob 1 motor 1 rotates as I want but also motor 2 but always backwards no matter what direction i turn the knob.

When I turn knob 2 then motor 2 turns as I want but also motor 1 but always backwards no matter what direction I turn knob 2.

so you need to change your code to recognize when the values from encoder 1 or 2 change and use that change to step both motors

Using an encoder library would make this simpler

This seems to be where A and B do opposite math (and possibly direction). Make one of these for each encoder (do not share this routine with both).

These static variables are shared by both encoders so 'both' positions change if either encoder is turned. Find an Encoder library where each encoder is a separate object with its own variables.

i believe this is the approach

    // calculate the number of steps to move each motor
    int steps1 = encoder1NewPos - encoder1Pos;
    int steps2 = encoder2NewPos - encoder2Pos;

    if (0 != steps1)  {
        motor1.move ( steps1);
        motor2.move (-steps1);
    }
    else if (0 != steps2)  {
        motor1.move (-steps2);
        motor2.move ( steps2);
    }

bear in mind step1 and step2 may be negative

here is the working code now, thanks for the tip about library for the encoder, the library named encoder worked fine

#include <AccelStepper.h>
#include <Encoder.h>

// Set up constants for the two stepper motors
const int motor1StepPin = 8;
const int motor1DirPin = 10;
const int motor2StepPin = 9;
const int motor2DirPin = 11;

// Set up constants for the two rotary encoders
const int encoder1PinA = 2;
const int encoder1PinB = 3;
const int encoder2PinA = 4;
const int encoder2PinB = 5;

// Create the two AccelStepper objects for the two motors
AccelStepper motor1(1, motor1StepPin, motor1DirPin);
AccelStepper motor2(1, motor2StepPin, motor2DirPin);

// Set up variables to keep track of the encoder positions
long encoder1Pos = 0;
long encoder2Pos = 0;

// Create two Encoder objects
Encoder encoder1(encoder1PinA, encoder1PinB);
Encoder encoder2(encoder2PinA, encoder2PinB);

void setup() {
  // Set the speed and acceleration of the motors
  motor1.setMaxSpeed(60);
  motor1.setAcceleration(100);
  motor2.setMaxSpeed(60);
  motor2.setAcceleration(100);
}

void loop() {
  // Read the current position of each rotary encoder
  long encoder1NewPos = encoder1.read();
  long encoder2NewPos = encoder2.read();

  // Calculate the number of steps to move each motor
  long steps1 = encoder1NewPos - encoder1Pos;
  long steps2 = encoder2NewPos - encoder2Pos;

  // Move the motors by the calculated number of steps
  motor1.move(steps1);
  motor2.move(steps2);

  // Update the current position of the encoders
  encoder1Pos = encoder1NewPos;
  encoder2Pos = encoder2NewPos;

  // Run the motors
  motor1.run();
  motor2.run();
}

You can simplify quite a bit by using the absolute positions directly rather than translating to relative movements:

void loop() {
  // Set the motors' targets to the new encoder positions
  motor1.moveTo(encoder1.read());
  motor2.moveTo(encoder2.read());

  // Run the motors
  motor1.run();
  motor2.run();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.