Follow focus but for the zoom ring..Can't figure out how to do it

Hi ! I'm currently using this script with a nema 17 and a tmc2209 to control the focus ring of my lens with a rotary encoder.

#include <ESP32Encoder.h>
#include <AccelStepper.h>

#define CLK 34  // CLK ENCODER
#define DT 35   // DT ENCODER
ESP32Encoder encoder;

////////////STEPPER FOCUS//////////////////////////////////
const int stepPin = 12;
const int directionPin = 13;

AccelStepper stepperFocus(AccelStepper::DRIVER, stepPin, directionPin);
#define enablePin1 14  ///

int motorSpeedFocus = 10000;  //maximum steps per second (about 5rps / at 8 microsteps)
int motorAccelFocus = 8600;   //steps/second/second to accelerate

///////////////STEPPER ZOOM////////////////
const int stepPin2 = 4;
const int directionPin2 = 26;

AccelStepper stepperZoom(AccelStepper::DRIVER, stepPin2, directionPin2);
#define enablePin2 25  ///

int motorSpeedZoom = 4800;  //maximum steps per second (about 5rps / at 8 microsteps)
int motorAccelZoom = 4800;  //steps/second/second to accelerate

void setup() {
  pinMode(PinSW1, INPUT_PULLUP);  //SW1 A2

  stepperFocus.setMaxSpeed(motorSpeedFocus);
  stepperFocus.setSpeed(motorSpeedFocus);
  stepperFocus.setAcceleration(motorAccelFocus);

  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);

  encoder.attachHalfQuad(DT, CLK);
  encoder.setCount(0);

  //Serial.println(mode);
  Serial.begin(115200);
}

void focuser() {
  long newPosition = encoder.getCount();
  stepperFocus.moveTo(newPosition * 10);
  stepperFocus.run();
}

void loop() {
  focuser();
}

Everything works fine but what I want to do is the same thing but for the zoom ring.

To sum up, I want to control the nema 17 stepper with the rotary encoder (not the speed, the position) and make the stepper stop when the lens zooms to the maximum (minimum too) so as not to damage it, but I can't manage it and don't know how to do it with accelstepper and an endstop because I can't find an example of someone who's done the same thing.

Thanks in advance and sorry for my english..

I did try a lot of thing actually but since I'm bad at English finding the right words is difficult sometimes. I was just trying to find the best way to do things after a lot of trial and error. Sorry for that.

There is NO reason not to command the stepper one step at a time and also test for the end stop switch. You do have a switch to stop when the limit of movement has been reached, I hope!

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