Opposite direction for long distances using a stepper motor

Hi everyone,

I’m currently using an Arduino Uno R3 with the Motor Shield v3 to control my traverse system. My code is running fine but I just have a problem when I’m entering long distances, above 400 mm my code is still running but the motor is turning in the opposition direction.

Any ideas? Is it coming from int/long declarations?

Thank you, I’m hoping to start my measurements today so any help would be very appreciated.

Frederick

#include <Stepper.h>

long in1Pin = 3;
long in2Pin = 11;
long in3Pin = 9;
long in4Pin = 8;
long dirA = 12;
long dirB = 13;

Stepper motor(200, dirA, dirB);  
 
void setup()
{
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  
  Serial.begin(9600);

  motor.setSpeed(150);
  
}

void launchMotor() {
  digitalWrite(in1Pin, HIGH);
  digitalWrite(in2Pin, HIGH);
  digitalWrite(in3Pin, LOW);
  digitalWrite(in4Pin, LOW);
}

void stopMotor() {
  digitalWrite(in1Pin, LOW);   
  digitalWrite(in2Pin, LOW);   
  digitalWrite(in3Pin, LOW);   
  digitalWrite(in4Pin, LOW);   
}

void loop()
{
  while (Serial.available() != 0) ;
  {
    Serial.println("Enter distance (mm)");
  }
   while (Serial.available() == 0) ; 
   {
    launchMotor();
    long distance = Serial.parseInt();
    long steps = -(180/1.8)*distance; 
    motor.step(steps);
    stopMotor();
    Serial.print("Done ");
    Serial.print(distance);
    Serial.print(" mm ");
    Serial.println();

    Serial.flush();
   }
}

What value do you see for the steps variable ?

  1. Pls post motor related question in its own category - that's why they are there to have a topic concentration in the right categories

  2. Don't use pin13 if you can avoid it; you never know what happens to your stepper when the arduino is booting up as the internal LED is active during boot and might send unwanted commands to your stepper if it is already powered.

  3. use unsigned variables for steps so that they can't get negative and change direction - if required - in your code by computing the CCW direction

As @UKHeliBob said: have a Serial.print() inserted for debugging and print the value of your variable after it has parsed the serial input.

  while (Serial.available() != 0) ;
  {
    Serial.println("Enter distance (mm)");
  }

What are the curly braces for?

While there is serial data available, do nothing. How is THAT useful? You are going to be f**ked if serial data IS available when that statement is executed.

   while (Serial.available() == 0) ;
   {
    launchMotor();
    long distance = Serial.parseInt();
    long steps = -(180/1.8)*distance;
    motor.step(steps);
    stopMotor();
    Serial.print("Done ");
    Serial.print(distance);
    Serial.print(" mm ");
    Serial.println();

    Serial.flush();
   }

{More} {useless} {curly} {braces}!.

Why is it necessary to wait for the serial data to have been sent?

The step() function only takes an int argument. If your value is below -32768 or above 32767 it will get truncated. You will have to break your motion up into movements of less than abut 32000 steps.

void longMove(Stepper *stepper, long steps) {
   while (steps > 32000) {
        stepper->step(32000);
        steps -= 32000;
    }
   while (steps < -32000) {
        stepper->step(-32000);
        steps += 32000;
    }
   if (steps != 0) 
     stepper->step(steps);
}