Accel Stepper Library with multiple positional moves

Hello,

I am trying to use the accel stepper library to drive a stepper to multiple positions using button presses and delays, although I am struggling with the Syntax of this library. I have it mostly working but am still hitting a few snags.

What is the standard syntax? (psuedo code):

1.) Move to a position away from zero after a button press

2.) Move to another position after waiting

3.) Move to another position after another button press, then immediately move back to zero

I keep trying to mix commands such as "setnewposition", distancetogo ==0, moveTo, runToNewPosition, and this is the best I have gotten, the MoveState loop will be recognized before homing, but not after the first two moving loops are executed. Thanks for any help!

// AccelStepper Setup


#include "AccelStepper.h"



AccelStepper stepperX (1, 4, 2) ; // 1 = Easy Driver interface
// NANO Pin 2 connected to STEP pin of Easy Driver
// NANO Pin 3 connected to DIR pin of Easy Driver

// Define the Pins used
const int home_limit = 8; // Pin 9 connected to Home Switch (MicroSwitch)
const int HomeSwitch = 9;
const int DriveSwitch = 10;
const int MoveSwitch = 11;


int home_limit_state = 0;
int home_state = 0;
int Drive_State = 0;
int MoveState = 0;


int homingcompleted = 0; // Integer to store the value declaring homing operation complete
int Drivecompleted = 0;


// Stepper Travel Variables


long initial_homing = 1; // Used to Home Stepper at startup


void setup() {
  Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds

  pinMode(home_limit, INPUT);
  pinMode(HomeSwitch, INPUT);
  pinMode(DriveSwitch, INPUT);
  pinMode(MoveSwitch, INPUT);

  delay(5);


  // Start Homing procedure of Stepper Motor at startup

  //Serial.print("Stepper is Homing . . . . . . . . . . . ");

}

void loop() {

  home_state = digitalRead(HomeSwitch);

  while (home_state == HIGH && homingcompleted == 0) {
    stepperX.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
    stepperX.setAcceleration(2000.0);  // Set Acceleration of Stepper

    while (digitalRead(home_limit)) {  // Make the Stepper move CCW until the switch is activated
      stepperX.moveTo(initial_homing);  // Set the position to move to
      initial_homing++;  // Decrease by 1 for next move if needed
      stepperX.run();  // Start moving the stepper
      delay(5);
    }

    stepperX.setCurrentPosition(0);  // Set the current position as zero for now
    stepperX.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
    stepperX.setAcceleration(2000.0);  // Set Acceleration of Stepper
    initial_homing = -1;

    while (!digitalRead(home_limit)) { // Make the Stepper move CW until the switch is deactivated
      stepperX.moveTo(initial_homing);
      stepperX.run();
      initial_homing--;
      delay(5);
    }

    stepperX.setCurrentPosition(0);
    Serial.println("Homing Completed");
    Serial.println("");

    homingcompleted = 1; // Change the homing completed variable so injection cannot be executed unless the stepper is homed
    Serial.println(homingcompleted);


  }

  Drive_State = digitalRead(DriveSwitch);
  //Serial.print(Drive_State);

  while (Drive_State == HIGH && homingcompleted == 1) {

    stepperX.setMaxSpeed(5000.0);      // Set Max Speed of Stepper (Faster for regular movements)
    stepperX.setAcceleration(1000.0);  // Set Acceleration of Stepper

    stepperX.moveTo(15500);
    stepperX.run();




    while (stepperX.distanceToGo() == 0) {

      delay(3000);

      stepperX.runToNewPosition(1000);  // WHY IS runToNewPosition NECESSARY?  WHY NOT moveTo?
      stepperX.run();
      Drivecompleted = 1;
    }




  }

  // THIS LOOP WILL NOT EXECUTE

  MoveState = digitalRead(MoveSwitch);
  Serial.print(MoveState);

  while (stepperX.distanceToGo() == 0 && MoveState == HIGH && Drivecompleted == 1) {

    stepperX.setMaxSpeed(5000.0);      // Set Max Speed of Stepper (Faster for regular movements)
    stepperX.setAcceleration(1000.0);  // Set Acceleration of Stepper


    stepperX.runToNewPosition(16000);
    stepperX.run();
    stepperX.runToNewPosition(10);
    stepperX.run();

    homingcompleted = 0; // Reset Homing Completed Variable
    initial_homing = 1;  // Reset initial homing variable
    Drivecompleted = 0;

  }

}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Also please use the AutoFormat tool to indent your code consistently for easier reading.

Your code is too long for me to study quickly without copying to my text editor.

In general you should not use WHILE loops with the AccelStepper library. Just put the stepperX.run(); as the last line in loop(), the library will stop the motor when the move is complete.

You can check distanceToGo() to verify when a move is complete.

Then you can use the function moveTo() to set a new absolute destination or use move() to set a relative destination

...R

Don't call setNewPosition() unless you really know what you are doing (this is the sort of thing you might use after homing calibration, though).

Make sure run() method is called very very frequently, then just use moveTo() and distanceToGo() to issue
commands and monitor progress. No calls to delay(), no while loops.

@Robin2

Fixed the syntax, thank you for your advice and patience.

Thanks for the information on the While loops? Any idea why this library doesn't like them?

It makes sense that they are incompatible, because the command "MoveState" will not even be read by the Serial monitor once the "DriveState" loop is entered. Suggesting that the code is trapped in that loop somehow.

For this code, I still need to use some form of conditional statements for inputs, driving commands. I have tried replacing the While statements with "if" statements and this causes total inaction.

Is there any way to use conditional inputs with this library? The stepper I am using is way too large (13Nm) to be compatible with the Arduino stepper.h library. Acceleration and Deceleration are essential.

amq353:
Thanks for the information on the While loops? Any idea why this library doesn't like them?

It's nothing specific to the library. Blocking code is just a bad idea.

For this code, I still need to use some form of conditional statements for inputs, driving commands. I have tried replacing the While statements with "if" statements and this causes total inaction.

Without seeing the latest version of your complete program I don't know what you have tried. The devil is in the detail.

...R