Integrating limit switches to stepper motor movement

Hello everyone,

To keep it brief I am having trouble integrating 2 limit switches into my project. I can't seem to figure it out. Below is the code. Basically what i am trying to do is add 2 limit switches (top and bottom of a rail like system) that will stop the motors when they are engaged. Both motors move simultaneously and in the same direction and are controlled with a joystick. The further the joystick is pushed in the +/- X-direction, the faster the motors move in the respective direction.

Thank you, any suggestions are greatly appreciated!

#include <AccelStepper.h> //accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper2(1, 10, 11); // direction Digital 11 (CCW), pulses Digital 10 (CLK)


//Pins
const byte Analog_X_pin = A0; //x-axis readings


//Variables
int Analog_X = 0; //x-axis value


int Analog_X_AVG = 0; //x-axis value average



void setup()
{

  //SERIAL
  Serial.begin(9600);
  //----------------------------------------------------------------------------    
  //PINS
  pinMode(Analog_X_pin, INPUT);
  
  //----------------------------------------------------------------------------  
  InitialValues(); //averaging the values of the pin (values from potmeter)
  //----------------------------------------------------------------------------  
  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second  
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  stepper.setSpeed(500);
  delay(500);
  //----------------------------------------------------------------------------
  stepper2.setMaxSpeed(5000); //SPEED = Steps / second  
  stepper2.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  stepper2.setSpeed(500);
  delay(500);  

}

void loop()
{
  ReadAnalog();  
  stepper.runSpeed(); //step the motors (this will step the motor by 1 step at each loop indefinitely)
  stepper2.runSpeed();  
}

void ReadAnalog()
{
  //Reading the potentiometer in the joystick: x.
  Analog_X = analogRead(Analog_X_pin);  
    

  //if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
  //This is a sort of a filter for the inaccuracy of the reading
  if(abs(Analog_X-Analog_X_AVG)>25) 
  {
  stepper.setSpeed(5*(Analog_X-Analog_X_AVG));    
  stepper2.setSpeed(5*(Analog_X-Analog_X_AVG));
  }
  else
  {
    stepper.setSpeed(0);
    stepper2.setSpeed(0);
  }
  //----------------------------------------------------------------------------  

}

void InitialValues()
{
  //Set the values to zero before averaging
  float tempX = 0;

  //----------------------------------------------------------------------------  
  //read the analog 50x, then calculate an average. 
  //they will be the reference values
  for(int i = 0; i<50; i++)
  {
     tempX += analogRead(Analog_X_pin);  
     delay(10); //allowing a little time between two readings
   
  }
  //----------------------------------------------------------------------------  
  Analog_X_AVG = tempX/50; 

  //----------------------------------------------------------------------------  
 
}

I think the issue maybe that accelstepper is a blocking function ( check !) so the program is held up until the stepper travel is over .
Hard wired limit switches that stop the power to the stepper is one solution , There maybe others such as small step , check limit switch , small step etc..

Write yourself a small sketch just looking at this issue ( eg in loop move the stepper , then light a led - to see if/how it holds up the code ) or google it .

Make sure your analog input from the limit switch ( why not digital ?) has a pull-up/down so the input is never open circuit

I don't see any attemped in your code to integrate limit switches. What did you try so far ?

This will never be used, because you only use runSpeed(), and runSpeed() doesn't implement acceleration.

The comment is wrong. runSpeed will not step the motor at each loop, but only if a step is due according to the actual speed set by `

No, the runSpeed method is not blocking. It only creates a step pulse if one is due according to the speed, or it does nothing.
The code contains no attempt to integrate limit switches.

1 Like

thank you for your feedback , I'll definitely look into the accelstepper blocking the function

This was my latest attempt at integrating just one limit switch. \

#include <AccelStepper.h> //accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper2(1, 10, 11); // direction Digital 11 (CCW), pulses Digital 10 (CLK)

//pins
const byte Analog_X_pin = A0; 

//Variables
int Analog_X = 0; //x-axis value
int LIMIT01= 0; // value 0 assigned to variable
int Analog_X_AVG = 0; //x-axis value average


void setup()
{

  //SERIAL
  Serial.begin(9600);
  
  //PINS
  pinMode(Analog_X_pin, INPUT);
  pinMode(6, INPUT); //pin 6 input
 
  //Stepper parameters
  //setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(5000); //SPEED = Steps / second  
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  stepper.setSpeed(500);
  delay(500);
  //----------------------------------------------------------------------------
  stepper2.setMaxSpeed(5000); //SPEED = Steps / second  
  stepper2.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  stepper2.setSpeed(500);
  delay(500);  

}

void loop()

{
  analogRead(Analog_X_pin);  
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
  stepper2.runSpeed(); 
}



void analogRead()
{
  if (digitalRead(LIMIT01)==LOW)
  {
  stepper.setSpeed(0);
  stepper2.setSpeed(0);
  }
  else
{
  analogRead(Analog_X_pin);  
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
  stepper2.runSpeed();  
}

void loop();
{
  //Reading the 3 potentiometers in the joystick: x, y and r.
  Analog_X = analogRead(Analog_X_pin);  
  LIMIT01 = digitalRead(6); //

  //if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
  //This is a sort of a filter for the inaccuracy of the reading
 
   if(abs(Analog_X-Analog_X_AVG)>25) 
    {
    stepper.setSpeed(5*(Analog_X-Analog_X_AVG));
    stepper2.setSpeed(5*(Analog_X-Analog_X_AVG));    
    }
    else
    {
      stepper.setSpeed(0);
      stepper2.setSpeed(0);
    }
} //cierre del void ReadAnalog
 }//cierre del else principal

i just have the following issues;

  1. As soon as the powers source is connected the motors begin to spin, without moving the joystick.
  2. Nothing is happening when the limit switch is pressed.

Thank you I'll look into all of these and get back to you with an update!

This code looks very confusing.

You call analogRead without assigning the returned value to any variable. So it is useless.

Here you are defining a additional analogRead function, which is different from the builtin analogRead, but has the same name. You should never do that it's only confusing. And you never call that function, so the complete code is dropped by the optimizer.

Why didn't you start from the working sketch posted in #1 and extend it only by reading and evaluating the switch additionally to reading the joystick? Without making so much senseless changes?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.