Problem with limiting my stepper motors movement radius

Hey guys,

as this is my first post here are some lines about me and my current project. I am a first semester economic computer science student with a huge interest in programming. With a good friend of mine I try to build a robotic arm with two axis. At the moment I have problems with limiting the movement radius of the two arms.

For the project I use an Arduino Uno with A4988 motor drivers and NEMA17 stepper motors. As the arm has a limited movement space, I want to define the stepper motors movement to 65 steps in both directions (left and right). After that the motor must not move anymore in either one of the directions, but the other.

In the code below I tried to count up the steps in a variable but if I push the joystick (which I use to control the arm) in one direction the variable counts up and the motor moves until I release the joystick. Only after I released the joystick the motor stops and I cannot move it again.

void loop() {

          while(stepper_counter_1 <= 65 and analogRead(X_Pin_1) > higher_mid){
              motor_movement_forward(X_Pin_1);
          }
}



void motor_movement_forward(int axis) {

      while(analogRead (axis) > highest){
      digitalWrite(dirPin_1,HIGH);
      digitalWrite(stepPin_1,HIGH); 
      delayMicroseconds(fast); 
      digitalWrite(stepPin_1,LOW); 
      delayMicroseconds(fast);
      stepper_counter_1++;
      Serial.println(stepper_counter_1);
      }

      while(analogRead (axis) > higher_mid and analogRead(axis) < highest){
        digitalWrite(dirPin_1,HIGH);
        digitalWrite(stepPin_1,HIGH); 
        delayMicroseconds(slow); 
        digitalWrite(stepPin_1,LOW); 
        delayMicroseconds(slow);
        stepper_counter_1++;
        Serial.println(stepper_counter_1);
      }
}

Instead of moving until I release the joystick, the motor should stop when the counter hits 65 even if the joystick is still pushed. Maybe the one or other has an idea on this? Of course I googled the problem first but didn’t found any solution similiar to my problem.

Thanks in advance!

You need to post a complete program.

A stepper motor has no means to know where it is so you almost certainly need at least one limit switch so that the Arduino can tell when the motor is at the HOME or ZERO position. Once that is established you can control the position by keeping count of the steps PROVIDED the motor is never overloaded so that it misses a step.

...R
Stepper Motor Basics
Simple Stepper Code

in motor_movement_forward() you have a while loop with no limit checking. Once it gets into the while loop it will stay there and keep counting until the analogRead is outside the window.

Thanks for the reply and also thanks for the links to your other topics, even if I didn't post the whole programm. There is a lot of other code in it that was not necessary for explaining my problem, so I thought I just post this clipping.

In your reply it seems like I need at least one limit switch in any case, is that right? There is no "code-solution" to this?

genes1s:
There is a lot of other code in it that was not necessary for explaining my problem, so I thought I just post this clipping.

The answer to that is to write a short complete program that illustrates the problem.

In your reply it seems like I need at least one limit switch in any case, is that right? There is no "code-solution" to this?

Yes
No "code-only-solution"

...R