Basic question about stepper motor code

Hello all,
I am new to the forum and arduino development.
So please bear with my newbee questions.

I have a project I am working on and I need a little guidance.

Basic rundown.
Arduino Uno running a Pololu stepper driver triggered by a thumb joystick.
I wrote the code to have 3 different speeds. as you will notice below.
Where I am stuck is I want to use the push button on the joystick to set its travel limits.

So move to point “A” push the button, record that “step” value then move to point “B” push the button and record that step value. Then use those 2 values as limits for movement.

Right now I am stuck at how to just limit the movement at a specific step.

My code now inits the program with the step value of 1025 - figure the middle of my travel. with 2050 being one end and 0 the other. So the first step is what code do I use to stop the motor at those limits and not let it move past that. Once I figure that out I think I can sort out how to change the limits with the button press.

Any help is much appreciated.

#define step_pin 3  // Pin 3 connected to Steps pin on EasyDriver
#define dir_pin 2   // Pin 2 connected to Direction pin
#define MS1 5       // Pin 5 connected to MS1 pin
#define MS2 4       // Pin 4 connected to MS2 pin
#define SLEEP 7     // Pin 7 connected to SLEEP pin
#define X_pin A0    // Pin A0 connected to joystick x axis
 
int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps = 1025; // Assumes the belt clip is in the Middle

void setup() {
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);
   
   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up

   Serial.begin(9600);
 
/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/
 
   
}
 
void loop() {
  while (analogRead(X_pin) >= 900 && analogRead(X_pin) <= 1024) {
    if (steps > 0) {
      digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(MS1, LOW);
      digitalWrite(MS2, LOW);
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps++;
    }

 } 


    while (analogRead(X_pin) >= 750 && analogRead(X_pin) <= 850) {
    if (steps > 0) {
      digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(MS1, HIGH);
      digitalWrite(MS2, LOW);
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps++;
    }

 }

   while (analogRead(X_pin) >= 501 && analogRead(X_pin) <= 749) {
    if (steps > 0) {
      digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(MS1, LOW);
      digitalWrite(MS2, HIGH);
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps++;
    }

 } 

// other direction
   

  while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
    if (steps > 0) {
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(MS1, LOW);
      digitalWrite(MS2, LOW);
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps--;
    } 
  } 

    while (analogRead(X_pin) >= 101 && analogRead(X_pin) <= 250) {
    if (steps > 0) {
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(MS1, HIGH);
      digitalWrite(MS2, LOW);
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps--;
    } 
  } 

      while (analogRead(X_pin) >= 251 && analogRead(X_pin) <= 425) {
    if (steps > 0) {
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(MS1, LOW);
      digitalWrite(MS2, HIGH);
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps--;
    } 
  }

 Serial.print(analogRead(X_pin));     
}

Use variables - one for the current desired position, one for the actual position and each step updates the actual position unless this takes it out of range. Then you uses changes in actual position to command the motor, but you still know where you should be.

I don't understand what you are doing with microstepping and all that repetitive code in loop () - is that just testcode?

The micro stepping was to move the motor at different rates based on Joystick position (3 groups, slow, medium, fast). I should have used a variable to assign each of those microstep values I just haven’t gotten to that yet.
I am sure there is a better way to do this as well. As I have seen others use some of the available libraries that give a more variable speed function, but as I am still testing and I have not gone that far yet.

The variable I use now for position is “steps”
I init this at startup as 1025. Lets assume this is the middle of the travel.
I then update the steps each movement with steps++ or steps–

I guess where I get confused is I can query that value
for example.

if (steps == 2050) { code here }

But what I haven’t figured out is what do I use after that to stop the motor, but allow me to still reverse direction.
I tried something like

 while (analogRead(X_pin) >= 900 && analogRead(X_pin) <= 1024) {  //joystick read and control
   if (steps > 0) {                     
     digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
     digitalWrite(MS1, LOW);   
     digitalWrite(MS2, LOW);      // full steps
     digitalWrite(step_pin, HIGH); 
     delay(1);
     digitalWrite(step_pin, LOW);
     delay(1);                           //write movement
     steps++;                          // add to the step value
   }

if (steps == 2050) {
     digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
     digitalWrite(step_pin, LOW);
     steps;
   } 
 }

What I assume is that this would stop the motor (using LOW) when steps reached 2050. And I see this happen with the serial monitor outputting the steps value.
But where I am stuck is when I does reach here it wont allow any further movement at all.
How to I get it to allow my to move back in the opposite direction.
Am I missing something really obvious?

I guess I could also change the first “if” line and remove the second all together to be

if (steps >= 0 && =< 2050)

that could solve the whole issue, am I correct?

thanks in advance for the help, like I said total newb here

Eric

So if I change the code to use

if (steps >= 0 && <= 2050)

then I get
error: expected primary-expression before ‘<=’ token

I will repost full code soon

I am an idiot…
I solved it with

while (analogRead(X_pin) >= 900 && analogRead(X_pin) <= 1024) {
    if (steps >= start)
    if (steps <= ending) {

@edehaven, use the code button </> for your code, not the quote button. Code should look like this and be easy to copy to a text editor to study.

Ideally, please correct your existing Posts.

…R

I am an idiot…

I won’t argue.

   while (analogRead(X_pin) >= 900 && analogRead(X_pin) <= 1024) 
   {
    if (steps >= start && steps <= ending)
    {

makes more sense to me, though.

Its all about learning.

We all start as idiots at the things we become good at ;-)