Problem with Limit Switches

Hi to everyone, my name is Luca and i am a newbie to Arduino, i have a problem with my project of a slider for Photography. The project is to use a Nema 17 with easy driver and 3 buttons (left, right, stop) to move the belt that makes the camera slide a potentiometer to control the speed and 2 limit switches to stop the motor. Everything works fine but the problem is that when a limit switch is hit, the platform stops but i am not able to move it again because the switch stays activated.
Can anyone help me to solve this problem? Thank you all :slight_smile:

Please post your code (in tags). Without your code, I don't think we can help.

You need to post your code, but one thing to look at is whether you are looking for a change in the switch (from not-active to active) or whether you are just working on the current state (active). You should only stop on the transition, not the level.

..or let the stop signal take effect only if the direction signal is CW/CCW

Hi, thank you for the replies :slight_smile:
I think that the solution to let the stop signal take effect only if the direction signal is CW/CCW is the right way to move :smiley: so when the platform, moving to the right, clicks the LimitDX the motor stops but lets me move the platform on the opposite way by clicking the left button.

Here is my code, any help will be appreciate.

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);

// Define our three input button pins
#define step_pin 9  // Pin 9 connected to Steps pin on EasyDriver
#define dir_pin 8   // Pin 8 connected to Direction pin

#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2

#define LimitDX 5  // Pin 5 connected to Right Limit switch out
#define LimitSX 6  // Pin 6 connected to Left Limit switch out

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 5000
#define  MIN_SPEED 0

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  // Set up the three button inputs, with pullups
  pinMode(LimitDX, INPUT);
  pinMode(LimitSX, INPUT);


void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  else if (digitalRead(RIGHT_PIN) == 0) {    
    sign = -1;
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  if (digitalRead(LimitDX) == 0) {
    sign = 0;
  if (digitalRead(LimitSX) == 0) {
    sign = 0;
  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).  
  if (analog_read_counter > 0) {
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    //  And scale the pot's value from min to max speeds
    current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
    // Update the stepper to run at this new speed

  // This will run the stepper at a constant speed

Hi to everyone, thinking of what i wrote before, i thought to change the following portion of code

if (digitalRead(LimitDX) == 0) {
    sign = 0;
  if (digitalRead(LimitSX) == 0 && (digitalRead() {
    sign = 0;

with this:

if (digitalRead(LimitDX) == 0 && digitalRead(step_pin) == 1); {
    sign = 0;
  if (digitalRead(LimitSX) == 0 && digitalRead(step_pin) == -1); {
    sign = 0;

But, how can i tell to ARDY that he can still move the motor to the opposite side of the clicked switch?

Instead, how can i tell ARDY that once the switch is clicked, his state have to change from true to false in order to let the motor move to the other side ?

Many thanks to everyone for any help :smiley: :smiley: :smiley:

Hi, anyone can give me a little help on this? :neutral_face: :kissing: :kissing: :kissing:

"digitalRead(step_pin)" is only every going to give 0 or 1, never -1. That should give you a clue.

I suspect you are actually wanting something like below. Please check that the signs are correct- I may have the left and right movements reversed with respect to the limit switches.

if (digitalRead(LimitDX) == 0 && sign == -1 ) {
    sign = 0;
if (digitalRead(LimitSX) == 0 && sign== 1) {
    sign = 0;

Please also note that I moved the semicolons to the right spot- in your original the blocks of code get executed regardless of the results of the tests.