Go Down

Topic: Stepper Motor Direction Control Lag with Joystick (Read 96 times) previous topic - next topic

Fielding

Hi All,

I am trying to control the direction a stepper motor turns with a Joystick. Im using a 28BYJ-48 stepper and the ULN2003 driver, and a standard 5 pin analog joystick, although not using the switch pin.
Basically Im trying to program the stepper to spin in one direction when the x axis on the Joystick is pushed one direction, and spin the other when the x axis is in the other direction.
Sketch Included below

Link to youtube video of the problem : https://www.youtube.com/watch?v=52lGWr7BL_Q&feature=youtu.be

Its mostly working as planned, however there is noticeable lag in the turn around time. As in, when I change the Joystick axis, the stepper continues in the same direction as before for several steps, slows, stops, then begins to spin the other way.

Is this a problem with the acceleration setting in the Accelstepper library, or has anyone come across this before. Its not a project ending issue, but for the sake of accuracy Id like to have finer control over the motor.

Thanks

Code: [Select]

#include <AccelStepper.h>

// Motor Pin Deinitions
#define motorPin1  3    // IN1 on the ULN2003 Driver
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5    // IN3 on the ULN2003 driver 1
#define motorPin4  6    // IN4 on the ULN2003 driver 1

int position_to_go_max = 6000;
int position_to_go_min = 0;// Arbitrary Position for motor to go

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(AccelStepper::FULL4WIRE, motorPin1, motorPin3, motorPin2, motorPin4);

//JOYSTICK Definitons

const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output
int X_val = 0;        // Value for X pos of JS
int Y_val = 0;        // Value for Y pos of JS


void setup() {
  //Stepper
  stepper1.setMaxSpeed(300);
  stepper1.setAcceleration(200);
  stepper1.setSpeed(300);
  stepper1.setCurrentPosition(3000);
  //stepper1.moveTo(position_to_go_max);

  //Joystick
  pinMode(X_pin, INPUT);
  pinMode(Y_pin, INPUT);


  Serial.begin(9600);

}

void loop() {
  X_val = analogRead(X_pin);
  Y_val = analogRead(Y_pin);
  Serial.println(X_val);



  if ( X_val < 450) {
    stepper1.run();
    stepper1.moveTo(position_to_go_min);
  }

  if ( X_val > 650) {
    stepper1.run();
    stepper1.moveTo(position_to_go_max);
  }




}

Robin2

#1
Aug 10, 2018, 08:40 pm Last Edit: Aug 10, 2018, 08:41 pm by Robin2
As in, when I change the Joystick axis, the stepper continues in the same direction as before for several steps, slows, stops, then begins to spin the other way.

Is this a problem with the acceleration setting in the Accelstepper library, or has anyone come across this before. Its not a project ending issue, but for the sake of accuracy Id like to have finer control over the motor.
It's not so much a problem as a feature. The purpose of using acceleration is because the motor may miss steps if you start it or stop it too quickly.

Have you tried increasing the acceleration rate?


Another thing - with your code it is possible that the line stepper1.moveTo(position_to_go_min); will be called several times in quick succession if the joystick stays in position. Is that what you want?

And way the more usual use of the Accelstepper library would be to put stepper.run() in loop() - like this
Code: [Select]
void loop() {
  X_val = analogRead(X_pin);
  Y_val = analogRead(Y_pin);
  Serial.println(X_val);

  if ( X_val < 450) {
    stepper1.moveTo(position_to_go_min);
  }

  if ( X_val > 650) {
    stepper1.moveTo(position_to_go_max);
  }
  stepper1.run();
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up