Push button to move stepper motor while blocking all other input? AccelStepper

I am attempting to implement a “Home” function on a camera slider using the AccelStepper library. This library allows me to quickly move a camera carriage a set number of steps in very little time. My issue is that with my push button implementations, I am always polling for other buttons to be pressed. I have tested a simple sketch out to move my carriage back and forth very quickly, and my hope was to use the “up” push button to call a homing function that would essentially run iterations of that simple sketch until the carriage was home, then return back to the main loop to see if a different push button was activated. The way I have it implemented now, I have to hold the “up” button down, and even then the movement is very slow. I believe the accellstepper library works by calling the run function very quickly and that is how it is able to achieve such high speeds? So if I’m not calling the goHome function over and over, I’m not getting the speed I saw from the simple sketch?

Here is the simple sketch I was using successfully to move carriage very quickly back and forth:

#include <AccelStepper.h>

AccelStepper stepper(1, 45, 44);
int pos = 7400;
int zero = 0;
int swap = 0;

void setup(){
  stepper.setMaxSpeed(10000);  //pretty fast for the rails.  still only pulled 650mA
  stepper.setAcceleration(5000);
}

void loop(){
  if (stepper.distanceToGo() == 0)
  {
    delay(3000);
    swap = pos;
    pos = zero;
    zero = swap;
    stepper.moveTo(pos);
  }
  stepper.run();
}

Here is my full sketch with a lower max speed, acceleration, and pos (steps to move):

#include <LiquidCrystal.h>
#include <AccelStepper.h>

//Pin assignments for buttons
const int selectButton = 2;
const int leftButton = 3;
const int upButton = 10;
const int downButton = 11;
const int rightButton = 12;
const int shutter = 13;
const int directionPin = 44;
const int stepPin = 45;
const int Steps = 50;  // how many steps to move the carriage each interval
const int Speed = 3600;  // how many steps at a time the motor will move
int pos = 400;  // this will be 7400 after testing.  don't want to risk crashing the carriage during testing

signed long Distance = 0;  //7700 is max on rails.  keep track of steps


//Pin assignments for LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//Pin assignments for stepper
AccelStepper stepper(1, 45, 44);

const int RELEASE_TIME = 100;      // shutter release time for camera

void setup()
{
pinMode(selectButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
digitalWrite(directionPin, LOW);  //LOW moves toward stepper motor;  High moves towards bearrings
digitalWrite(stepPin, LOW);
pinMode(shutter, OUTPUT);  //initialize output pin for camera release

stepper.setMaxSpeed(2000);
stepper.setAcceleration(1000);

lcd.clear();
}

void goHome() //function to be called to return carriage to starting point
 {
  if (stepper.distanceToGo() == 0)
  {
    delay(2000);
    stepper.moveTo(pos);
  }
  stepper.run();
}

void loop()
{
  if(digitalRead(selectButton) == LOW)  //safety "pause" button in case carriage moves in unexpected manner
  {
    digitalWrite(directionPin, LOW);
    digitalWrite(stepPin, LOW);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("You Pressed The:");
    lcd.setCursor(0,1);
    lcd.print("Pause Button");
    delay(5000);
  }
  if(digitalRead(leftButton) == LOW)  //this will later be used to automatically move the camera "Steps" toward bearrings
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Current Distance");
    lcd.setCursor(0,1);
    lcd.print( Distance );
    digitalWrite(directionPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(Speed);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(Speed);
    Distance--;
  }
  if(digitalRead(upButton) == LOW)  //reset carriage back to home position at far end of rails
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Moving to Home");
    lcd.setCursor(0,1);
    lcd.print("Position");
    goHome();
  }
  if(digitalRead(downButton) == LOW)  //reset current distance back to 0
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Set Distance = 0");
    lcd.setCursor(0,1);
    Distance = 0;
    lcd.print("Distance: ");
    lcd.print( Distance );
  }
  if(digitalRead(rightButton) == LOW)  //this will later be used to automatically move the camera "Steps" toward the motor
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Current Distance");
    lcd.setCursor(0,1);
    lcd.print( Distance );
    digitalWrite(directionPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(Speed);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(Speed);
    Distance++;
  }
}

My biggest question is how to execute the goHome function exclusively until the carriage has arrived back at it’s starting point. Later I will implement limit switches to do that step for me automatically, but they haven’t arrived yet, and I still don’t know how to keep calling the goHome function until the carriage has reached it’s starting point.

From a glance, try adding a state variable for moving home. Something like...
If movinghome == true { Call your go home function }

Or use your go home function to return a true/false which then you determine rather to go into other ifs or nothing.

Sorry for not formatting correctly, some reason mobile doesn't give me the editor, but I think it's nothing you are not already capable of. Just haven't seen it yet :slight_smile:

My biggest question is how to execute the goHome function exclusively until the carriage has arrived back at it's starting point.

Make goHome() a blocking function. There is no reason to call goHome() over and over from loop() when you don't want loop() to do anything else.

If you have this code figured out, could you post what you did to get it working the way you wanted? I am working on something that is implementing some of the ideas you have in your code and homing is one that I have been struggling with.

thanks for anything you can offer!