Canon Zoom Mechanism

Hi Guys
I am making a Zoom attachment for a Canon lens. I use a Stepper motor to rotate a worm drive that moves the lens in and out. I have limit switches each end of the worm drive that connects to 0v if activated. I use a smart phone with a BlueTooth link to control the speed, start and stop of the stepper motor.
The code I have has three main parts the first part are two “while” loops that moves the lens to the sart position until it activates the right hand or start limit switch it then moves away from the right hand limit switch when the limit switch is at logic high it stops the motor and waits for a signal from the smart phone. This all seems to work OK.
The second section is to monitor the limit switches to stop the motor if I send it too far left or right with the smart phone. The requirement is to rotate the stepper motor in the correct direction to move it away from the limit switches if ether of the limit switches are activated.

The third section is the communication with the smart phone this seems to work OK.

The problem I have is that after the first two “While” loops have finished and the system is waiting for a command from the smart phone if I activating ether of the limit switches the motor speed has now changed and I only get about 5 steps a second. Sometimes the motor speed is OK and is rotating the motor at the same speed as it did during the “While” loops but most of the time it is much slower.
The code for the second stepper motor I think is OK but I have left it in situ for completeness.
Any help would be much appreciated I have spent weeks getting this far and am stuck.
Many Thanks.

#include <AccelStepper.h>
#include <SoftwareSerial.h>
// AccelStepper Setup
AccelStepper stepper1(1, 10, 9);   // 1 = Easy Driver interface 10 is Dir 9 is Step
AccelStepper stepper2(1, 7, 6); // dir is 7 step is 6 pin of Easy Driver
SoftwareSerial BTserial(0, 1); // RX | TX

// Define the Pins used
// Stepper Motor 1
#define Limit_L 2      // Pin 5 connected to Left Limit switch (MicroSwitch)
#define Limit_R 5      // Pin 2 connected to Right limit switch
#define MS1 4          // Pin 4 connected to MS1 pin of Easy driver 1
#define MS2 11         // Pin 11 connected to MS2 pin of Easy driver 1
// Stepper Motor 2
#define MS1_2 8        // Pin 8 connected to MS1 pin of Easy driver 2
#define MS2_2 3        // Pin 3 connected to MS2 pin of Easy driver 2

int spd = 1000;          // The current speed in steps/second for stepper 1
int sign = 0;            // Either 1, 0 or -1
int spd_2 = 1000;         // The current speed in steps/second for stepper 2
int sign_2 = 0;           // Either 1, 0 or -1

boolean Left_buttonWasPressed;
int Left_buttonState;
boolean Right_buttonWasPressed;
int Right_buttonState;
long initial_homing = 0; // Used to Home Stepper at startup

void setup() {
  Serial.begin(9600);
  pinMode(Limit_L, INPUT_PULLUP);
  pinMode(Limit_R, INPUT_PULLUP);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(MS1_2, OUTPUT);
  pinMode(MS2_2, OUTPUT);

  digitalWrite(MS1, LOW);      // Configures to Full Steps
  digitalWrite(MS2, LOW);      // Configures to Full Steps

  //  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepper1.setMaxSpeed(200.0);      // Set Max Speed of Stepper for Zoom
  stepper1.setAcceleration(200.0);  // Set Acceleration of Stepper for Zoom

  stepper2.setMaxSpeed(1000.0);      // Set Max Speed of Stepper for Focus
  stepper2.setAcceleration(100.0);  // Set Acceleration of Stepper for Focus

  BTserial.begin(9600);
  Left_buttonWasPressed = true;
  Right_buttonWasPressed = false;

  while (digitalRead(Limit_R)) {  // Make the stepper1 move CW until the switch is activated
    stepper1.moveTo(initial_homing);  // Set the position to move to
    initial_homing++;  // increase by 1 for next move if needed
    stepper1.run();  // Start moving the stepper 1
    delay(2);
  }

  stepper1.setCurrentPosition(0);  // Set the current position as zero for now
  initial_homing = 1;

  while (!digitalRead(Limit_R)) { // Make the Stepper move CCW until the switch is deactivated
    stepper1.moveTo(initial_homing);
    stepper1.run();
    initial_homing--;
    delay(2);

  }

}

void loop() {
  //stepper1.setMaxSpeed(100.0);      // Set Max Speed of Stepper for Zoom
  //stepper1.setAcceleration(100.0);  // Set Acceleration of Stepper for Zoom

  // read the state of the pushbutton value:
  Left_buttonState = digitalRead(Limit_L);  // deal with initial press
  if (Left_buttonState == LOW && Left_buttonWasPressed == false) {
    stepper1.moveTo(10000L);
    Left_buttonWasPressed = true;
  }
  else if (Left_buttonState == LOW && Left_buttonWasPressed) { // deal with the button being held down
    stepper1.run();
  }
  else if (Left_buttonState == HIGH && Left_buttonWasPressed) {  // deal with the button being released
    stepper1.stop();
    while (stepper1.distanceToGo() != 0) {
      stepper1.run();
    }
    Left_buttonWasPressed = false;   // reset the button so this doesn't trigger again next loop
  }
  Right_buttonState = digitalRead(Limit_R);  // read the state of the pushbutton value:
  if (Right_buttonState == LOW && Right_buttonWasPressed == false) { // deal with initial press
    stepper1.moveTo(-10000L);
    Right_buttonWasPressed = true;
  }
  else if (Right_buttonState == LOW && Right_buttonWasPressed) {   // deal with the button being held down
    stepper1.run();
  }
  else if (Right_buttonState == HIGH && Right_buttonWasPressed) {  // deal with the button being released
    stepper1.stop();
    while (stepper1.distanceToGo() != 0) {
      stepper1.run();
    }
    Right_buttonWasPressed = false;  // reset the button so this doesn't trigger again next loop
  }

  char c;
  if (BTserial.available()) {

    c = BTserial.read();
    Serial.write(c);
    if (c == 'i') {  // Zoom in
      sign_2 = -1;
    }
    if (c == 'o') {  // Zoom out
      sign_2 = 1;
    }
    if (c == 's') {  // halt
      sign_2 = 0;
    }
    if (c == 'f') {  // forward
      sign = 1;
    }
    if (c == 'r') {  // reverse
      sign = -1;
    }
    if (c == 's') {  // stop
      sign = 0;
    }
    if (c == '1') {  // super slow
      spd = 10;
    }
    if (c == '2') {  // slow
      spd = 100;
    }
    if (c == '3') {  // medium
      spd = 300;
    }
    if (c == '4') {  // Fast
      spd = 500;
    }
    if (c == '5') {  // Faster
      spd = 700;
    }
    if (c == '6') {  // Very fast
      spd = 1000;
    }
    if (c == '1') {  // super slow
      spd_2 = 20;
    }
    if (c == '2') {  // slow
      spd_2 = 30;
    }
    if (c == '3') {  // medium
      spd_2  = 40;
    }
    if (c == '4') {  // Fast
      spd_2 = 50;
    }
    if (c == '5') {  // Faster
      spd_2 = 80;
    }
    if (c == '6') {  // Very fast
      spd_2 = 100;
    }

    stepper2.setSpeed(sign * spd);
    stepper1.setSpeed(sign_2 * spd_2);

    if (Serial.available())
    {
      c =  Serial.read();
      BTserial.write(c);
    }
  }
  stepper2.runSpeed();
  stepper2.setAcceleration(100.0);  // Set Acceleration of Stepper for Zoom
  stepper1.runSpeed();
  stepper1.setAcceleration(100.0);  // Set Acceleration of Stepper for Zoom

}

You seem to have several ELSE IF clauses ad it is not clear what they belong to. Use the AutoFormat tool to indent your code consistently and it will be easier to read. Then post the revised version.

...R