Controlling a Stepper Motor to run a certain distance

I am confused on why my code is skipping a couple of lines. Below is a set of bullet points its supposed to do. But when I run my code it skips steps 4 and 7. I'm not sure why its skipping.

  1. Start Button is hit to start Loop
  2. Stepper goes until LimitSwitch2 is hit at fast speed
  3. Waits a second
  4. Stepper drives down a certain distance (1ft) at fast speed
  5. Stepper then drives down until LimitSwitch1 is hit at slower speed
  6. Waits a second
  7. Stepper then reverses a certain distance (1/2in) at slow speed
  8. Stepper then reverses until LimitSwitch2 is hit at fast speed
  9. End loop

I have an arduino uno, 2 Nema 23 motors and 2 TB6600 driver.
My Code:

// Define stepper motor connections and steps per revolution:
#define dirPin1 3
#define stepPin1 2
#define dirPin2 5
#define stepPin2 4
#define stepsPerRevolution 3200
#define limitPin1 8
#define limitPin2 9
#define startPin 10
#define downPin 6
#define upPin 7

void setup() {
// Declare pins as output:
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(limitPin1, INPUT);
pinMode(limitPin2, INPUT);
pinMode(startPin, INPUT);
pinMode(downPin, INPUT);
pinMode(upPin, INPUT);
}

void loop() {
////////////////////////////////////////////////////// Auto Movement
{ if(digitalRead(startPin) == LOW)

{ for (int i = 0; i < stepsPerRevolution*50000 && digitalRead(limitPin2); i++) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}

delay(1000);

// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution*30000; i++) {
// Motor 1
digitalWrite(dirPin1, LOW);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
// Motor 2
digitalWrite(dirPin2, LOW);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}

delay(1000);

for (int i = 0; i < stepsPerRevolution*50000 && digitalRead(limitPin1); i++) {
// Motor 1
digitalWrite(dirPin1, LOW);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin1, LOW);
delayMicroseconds(15);
// Motor 2
digitalWrite(dirPin2, LOW);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin2, LOW);
delayMicroseconds(15);
}

delay(1000);

for (int i = 0; i < stepsPerRevolution; i++) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin1, LOW);
delayMicroseconds(15);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin2, LOW);
delayMicroseconds(15);
}

delay(1000);

for (int i = 0; i < stepsPerRevolution*50000 && digitalRead(limitPin2); i++) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}}}

//////////////////////////////////////// Maunal Movement
{ if(digitalRead(downPin) == LOW) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin1, LOW);
delayMicroseconds(7);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin2, LOW);
delayMicroseconds(7);
}

if(digitalRead(upPin) == LOW) {
// Motor 1
digitalWrite(dirPin1, LOW);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin1, LOW);
delayMicroseconds(7);
// Motor 2
digitalWrite(dirPin2, LOW);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin2, LOW);
delayMicroseconds(7);
}}}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please edit your post to add code tags (select, use the "</>" editor button.

Better, reformat your code in the Arduino IDE, so it is indented sensibly. Use CTRL-T

That an awfully big number.

might be too fast!
try 2 msec

There are good reasons to drive steppers the way you are, but you’ll save a lot of time and pain if you try the AccelStepper library in the IDE toolkit.

I run 4 motors independently on a UNO - the only limitation being the max step rate per second ~4000 (due to the library complexity and cpu clock)

Still not got it?
I'll highlight the problem.

Your Step 8 makes no sense. You were going DOWN in Step 5 so when you 'reverse' in Step 7 you are going UP. If you reverse again in Step 8 you are going DOWN again and heading toward LimitSwitch1. You are never going to reach LimitSwitch2.

Rather than saying 'goes' and 'reverses', always say DOWN and UP.

Instead of calling the limit switches 1 and 2, call them DownLimit and UpLimit or BottomLimit and TopLimit.

// Define stepper motor connections and steps per revolution:
const byte dirPin1 = 3;
const byte stepPin1 = 2;
const byte dirPin2 = 5;
const byte stepPin2 = 4;

const byte limitPin1 = 8;
const byte limitPin2 = 9;
const byte startPin = 10;
const byte downPin = 6;
const byte upPin = 7;

const unsigned stepsPerRevolution = 3200;
const unsigned long REVOLUTIONS_PER_FOOT = 50000ul;
const unsigned long STEPS_PER_FOOT = stepsPerRevolution * REVOLUTIONS_PER_FOOT;

const int DIR_1_UP = HIGH;
const int DIR_1_DOWN = LOW;
const int DIR_2_UP = HIGH;
const int DIR_2_DOWN = LOW;

void setup()
{
  // Declare pins as output:
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(limitPin1, INPUT_PULLUP);
  pinMode(limitPin2, INPUT_PULLUP);
  pinMode(startPin, INPUT_PULLUP);
  pinMode(downPin, INPUT_PULLUP);
  pinMode(upPin, INPUT_PULLUP);
}

void loop()
{
  // Start Button is hit to start Loop
  while (digitalRead(startPin) == HIGH)
  {
    /* Waiting... */
  }

  // Stepper goes (up?) until LimitSwitch2 is hit at fast speed
  digitalWrite(dirPin1, DIR_1_UP);
  digitalWrite(dirPin2, DIR_2_UP);
  while (digitalRead(limitPin2) == HIGH)
  {
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(5);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(5);
  }

  // Waits a second
  delay(1000);

  // Stepper drives down a certain distance (1ft) at fast speed
  digitalWrite(dirPin1, DIR_1_DOWN);
  digitalWrite(dirPin2, DIR_2_DOWN);
  for (unsigned long i = 0; i < STEPS_PER_FOOT; i++)
  {
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(5);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(5);
  }

  // Stepper then drives down until LimitSwitch1 is hit at slower speed
  digitalWrite(dirPin1, DIR_1_DOWN);
  digitalWrite(dirPin2, DIR_2_DOWN);
  while (digitalRead(limitPin1) == HIGH)
  {
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(5);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(5);

    delay(100);  // Slower, About 10 steps per second
  }

  // Waits a second
  delay(1000);

  // Stepper then reverses (up?) a certain distance (1/2in) at slow speed
  digitalWrite(dirPin1, DIR_1_UP);
  digitalWrite(dirPin2, DIR_2_UP);
  for (unsigned long i = 0; i < STEPS_PER_FOOT / 24; i++)
  {
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(5);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(5);

    delay(100);  // Slower, About 10 steps per second
  }

  //Stepper then reverses (DOWN???) until LimitSwitch2 is hit at fast speed
  ///// ??? Limit Switch 2 is UP, not DOWN.
  digitalWrite(dirPin1, DIR_1_DOWN);
  digitalWrite(dirPin2, DIR_2_DOWN);
  while (digitalRead(limitPin2) == HIGH)
  {
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(5);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(5);
  }
  
  //End loop
}

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