Stepper Motor Delay is Inconsistent

Hi All,
I have a problem I can't figure out. I have a stepper motor I would like to move every ~2.5 minutes if a limit switch is pressed. If the limit switch continues to be pressed I would like for it to follow this cycle. If the limit switch is not pressed, the motor would not spin. The problem I have is that the delay is very inconsistent. Sometimes it will spin in under a minute, and others it will take 8+ minutes to have the motor spin (if at all), all while the limit switch remains pressed. Would anyone take a look at my code and let me know where I might be going wrong? I very new to Arduino Programming (~4 Months), so any direction or advice would be greatly appreciated. Thanks!

These are the components I'm using
Arduino Nano Mega328p
Limit switch
5v Stepper
ULN2003 Driver Board

#include <Stepper.h>

const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
const int limitPin = 7;
int StepsRequired;
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

void setup()
{
  pinMode(limitPin, INPUT);
}

void loop()
{
  if ( digitalRead(limitPin) == HIGH) {

    // Rotate CCW
    StepsRequired  =  - STEPS_PER_OUT_REV / 4;
    steppermotor.setSpeed(600);
    steppermotor.step(StepsRequired);
    delay(500);

    // Rotate CW
    StepsRequired  =  STEPS_PER_OUT_REV / 4;
    steppermotor.setSpeed(600);
    steppermotor.step(StepsRequired);
    delay(500);

    // Rotate CCW
    StepsRequired  =  - STEPS_PER_OUT_REV / 4;
    steppermotor.setSpeed(600);
    steppermotor.step(StepsRequired);
    delay(500);

    // Rotate CW
    StepsRequired  =  STEPS_PER_OUT_REV / 4;
    steppermotor.setSpeed(600);
    steppermotor.step(StepsRequired);
    delay(135000);
  }
}

When the limit switch is not pressed it is held at ground. When the limit switch is pressed it will float since there is no pullup or pulldown resistor. The state of a floating switch is indeterminate (random). I would wire the switch to ground and the NO terminal to an input set to INPUT_PULLUP. The switch would then read HIGH when not pressed and LOW when pressed. Also I would put a 0.1uF ceramic cap across the switch to provide some hardware debounce and prevent false readings due to noise on the wire. If the wire is long the internal pullup may not be enough. Then use an external pullup, start with 10K and go down in value.

image

You are better off using millis() or micros() for timing. Non-blocking timing using millis() tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

as @groundFungus suggests, configure the limitPin as INPUT_PULLUP and wire the switch between the pin and ground.

however, i'm confused because normally a "limit" switch detects that something is at the limits of its travel or a specific position when "pressed".

and when used, a stepper motor would be stepped one step at a time and suspending this operation when the limit switch is reached.

it seems like you could use the limit switch to change the direction of the motor or stop.

and also as @groundFungus suggested, you could use a timer (i.e. millis()) to restart your sequence once the sequence is ended

Thanks @groundFungus! This is some awesome information. I'm going to try this out tonight. I really appreciate you including the schematic for the limit switch!

Thanks @gcjr I have seen some examples where a limit switch was used to determine the rotation of a stepper. I'm also going to look into removeing the delay() function from the code. Millis() seems to be a much better option.

That was the issue! Thanks for your help on this. Really appreciate it!

1 Like

Glad that i could be of assistance.

1 Like

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