Changing Direction?

5,999 views
Skip to first unread message

Michael Ivison

unread,
Mar 28, 2015, 1:08:53 AM3/28/15
to accels...@googlegroups.com
could someone tell me how to change the  direction of my stepper with this code? what i want to do later is to add another button to make the stepper go the other way momentarily. right now it goes CCW. i'm wanting to add this if statement in another sketch later.
[code]

const int buttonPin = 12;

#include <AccelStepper.h>
int buttonState = 0;
AccelStepper stepper(1, 9, 8 );
void setup()

   pinMode(buttonPin, INPUT);
  stepper.setMaxSpeed(10000);
   stepper.setSpeed(6000);
}

void loop()

buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
 
  stepper.runSpeed();
}

}

[/code]

Mike McCauley

unread,
Mar 28, 2015, 1:13:23 AM3/28/15
to accels...@googlegroups.com
On Friday, March 27, 2015 10:08:53 PM Michael Ivison wrote:
> could someone tell me how to change the direction of my stepper with this
> code?

Set a negative speed.


> what i want to do later is to add another button to make the stepper
> go the other way momentarily. right now it goes CCW. i'm wanting to add
> this if statement in another sketch later.
> [code]
>
> const int buttonPin = 12;
>
> #include <AccelStepper.h>
> int buttonState = 0;
> AccelStepper stepper(1, 9, 8 );
> void setup()
> {
> pinMode(buttonPin, INPUT);
> stepper.setMaxSpeed(10000);
> stepper.setSpeed(6000);
> }
>
> void loop()
> {
> buttonState = digitalRead(buttonPin);
> if (buttonState == HIGH){
>
> stepper.runSpeed();
> }
>
> }
>
> [/code]

--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474

Michael Ivison

unread,
Mar 28, 2015, 2:07:40 AM3/28/15
to accels...@googlegroups.com
Could you give me an example?

Mike McCauley

unread,
Mar 28, 2015, 2:11:12 AM3/28/15
to accels...@googlegroups.com

stepper.setSpeed(-6000);

Michael Ivison

unread,
Mar 28, 2015, 2:20:45 AM3/28/15
to accels...@googlegroups.com
Hallelujah!!!!! THANK YOU!!!!!


On Saturday, March 28, 2015 at 12:08:53 AM UTC-5, Michael Ivison wrote:

Dennis

unread,
Mar 30, 2015, 1:34:12 AM3/30/15
to accels...@googlegroups.com
Hi guys,

Could someone please help me understand what I might be missing? Am trying to control a stepper motor using a joystick. I read the analog values. If the value is less than the neutral position, motor rotates counterclockwise, if greater, motor rotates clockwise, otherwise it stays stationary. I've tried a few ways but no luck. The motor keeps turning in the same direction. Here's the code:

    if(!isXNeutral(x)){
      if((x - X_MIN) < 0){  //move left
          if (stepper1.distanceToGo() == 0)  {
             //stepper1.moveTo(pos);
          }
          else{
            stepper1.setSpeed(-900);
            //stepper1.stop();
            //delayMicroseconds(1000);
            //tempX = -pos;
            //stepper1.moveTo(tempX);
          }
      }
     
      else{  //move right
         if (stepper1.distanceToGo() == 0)  {
           // stepper1.moveTo(pos);
          }
         else{
           stepper1.moveTo(pos);
          }
        }
        stepper1.run();
    }

bool isXNeutral(int x){
  return (x > X_MIN && x < X_MAX)? true : false;
}

gregor christandl

unread,
Mar 30, 2015, 1:43:46 AM3/30/15
to accels...@googlegroups.com

only use setSpeed() when you use runSpeed().
In the "move left" part of the code, you do not set a target for the stepper. use:
stepper1.moveTo(-pos);

Mike McCauley

unread,
Mar 30, 2015, 1:48:54 AM3/30/15
to accels...@googlegroups.com
Hello,

if you want to use fixed speeds, you need to use runSpeed() not run().
If you want to use accelerations (with run()), you should be setting moveTo
with negative and positive positions.

Dennis

unread,
Mar 30, 2015, 7:59:53 PM3/30/15
to accels...@googlegroups.com
Thanks for your input. I made changes as suggested, but the motor still turns in the same direction. Cant explain why. Any help is greatly appreciated. I've attached the latest code below:

int pos = 100000;

AccelStepper stepper1(1, M1_STEP, M1_DIR);

void setup() { 
  stepper1.setMaxSpeed(1000);
  stepper1.setSpeed(900);
}

void loop() {
  x = analogRead(X_VAL);  //read x value from joystick
  sw = analogRead(SWITCH); //check if switch is pressed
 
  if(sw > 0){  //switch is not pressed, keep running motors
   
    if ( !isXNeutral(x) ){

      if ( (x - X_MIN) < 0) {  //move left
          if (stepper1.distanceToGo() == 0)  {
            //dont move, we've arrived at the desired position
          }

          else{ //move in opposite direction
            stepper1.moveTo(-pos);
          }
      }
     
      else{  //move right
         if ( !(stepper1.distanceToGo() == 0) )  {
            stepper1.moveTo(pos);
          }
          else { //dont move. stay stationary
           }
        }
        stepper1.runSpeed();

Mike McCauley

unread,
Mar 30, 2015, 10:53:55 PM3/30/15
to accels...@googlegroups.com
Suggest you start with the examples and prove they work with your motor.

Then decide whether you want accelerations or not.

If you want accelerations use in your code combinations of

move()
moveTo()
with run();

If you dont want accelerations, use
setSpeed()
with runSpeed()

dont mix up the 2 concepts.

Dennis

unread,
Mar 31, 2015, 12:48:57 AM3/31/15
to accels...@googlegroups.com
Thanks for your response mikem. As for your suggestion, I have tried another example and I know that the motor  does work. This code below runs fine:

 if (stepper1.distanceToGo() == 0)  {
    //delay(500);
    pos = -pos;
    stepper1.moveTo(pos);
  }

The motor runs to the very end and then reverses direction. In my earlier post, I tried having the motor reverse direction while it still had some distance to go (i.e. distanceToGo() > 0). That doesnt seem to work for me despite many efforts. I do want constant speed so I use setSpeed() awith runSpeed()

Mike McCauley

unread,
Mar 31, 2015, 12:55:10 AM3/31/15
to accels...@googlegroups.com
Look also at runSpeedToPosition().

Im about to leave for 6 weeks travel, so I wont be able to provide any more
free support until after then.

Cheers.

Sandy Noble

unread,
Mar 31, 2015, 4:09:29 AM3/31/15
to accels...@googlegroups.com

Have a good trip Mike. Dennis, what values does the joystick return? I don't think I've seen the full set of code in this thread yet, what X_MIN and X_MAX are set to.

The problem has to be on your line

if ( (x - X_MIN) < 0) {  //move left

Without knowing what the values of these bars are, I could only speculate about is happening, except that I guess that  (x - X_MIN) is never less than 0.

The easiest way to diagnose this is to add some debug Serial.println()s into your code to see what those values really are, and watch the serial console.

Sn

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Dennis

unread,
Mar 31, 2015, 9:46:34 PM3/31/15
to accels...@googlegroups.com
Thanks for your suggestion Sandy. I've tested the values am returning. They are negative and positive, as expected, depending on which way I move the joystick.

Here is the code:


#include <AccelStepper.h>

const int M1_STEP = 9;  //Motor 1 step
const int M1_DIR = 8;  //Motor 1 direction
const int M2_STEP = 10;  //Motor 2 step
const int M2_DIR = 7;    //Motor 2 direction
const int X_VAL = A0;
const int Y_VAL = A1;
const int SWITCH = A2;
const int X_MIN = 490;  //minimum possible x-axis value of joystick observed
const int X_MAX = 505;  //maximum possible x-axis value of joystick observed

AccelStepper stepper1(1, M1_STEP, M1_DIR);
AccelStepper stepper2(1, M2_STEP, M2_DIR);

int pos = 100000;
int x = 0;
int y = 0;
int sw = 0;
int tempX = 0;

//stepper1 is X motor

void setup() {
  pinMode(M1_STEP, OUTPUT);
  pinMode(M1_DIR, OUTPUT);
  pinMode(X_VAL, INPUT);   
 
  stepper1.setMaxSpeed(1000);
  stepper1.setSpeed(900);
   
  Serial.begin(9600);

}

void loop() {
  x = analogRead(X_VAL);  //read x value from joystick
  y = analogRead(Y_VAL);  //read y value from joystick

  sw = analogRead(SWITCH); //check if switch is pressed
 
  if(sw > 0){  //switch is not pressed, keep running motors
   
    if(!isXNeutral(x)){
      if((x - X_MIN) < 0){  //move left
          if (stepper1.distanceToGo() == 0)  {
           
          }
          else{           
            stepper1.moveTo(-pos);
            stepper1.setSpeed(900);
            //stepper1.runSpeed();
          }
      }
     
      else{  //move right
         if (!(stepper1.distanceToGo() == 0))  {
            stepper1.moveTo(pos);
            //stepper1.runSpeed();
          }
         
        }
       //Serial.println(x - X_MIN);
        stepper1.runSpeed();
    }
   
  }
 
}

bool isXNeutral(int x){
  return (x >= X_MIN && x <= X_MAX)? true : false;
}

Sandy Noble

unread,
Apr 1, 2015, 2:43:57 PM4/1/15
to accels...@googlegroups.com
Ok, like Mike has said in his previous emails, you need to choose
whether you want acceleration or not.

If wanting acceleration, use move() (or moveTo()) and then run().
If wanting steady speed, use setSpeed() and then runSpeed().

In your first case (move left), then you do

stepper1.moveTo(-pos);
stepper1.setSpeed(900);

So moveTo() sets the target to -100000, but then also sets the speed
to 900. Speed 900 is forwards, so it will never move to a negative
position. Try setSpeed(-900).

In your second case (move right) you are setting a positive target.
Using runSpeed() does not implement accelerations, so the speed will
never increase from _very slow_.

I'm a bit confused about the naming here. X_MIN and X_MAX are
described as being the minimum and maximum values, but they are used
as being the min and max of a "dead zone", a neutral area in the
middle (I assume).

I think this below is a more succinct way of putting what I _think_
you might mean to do:

if (!isXNeutral(x)) {
if (x < X_MIN) { //move left
stepper1.setSpeed(-900);
stepper1.runSpeed();
}
else if (x > X_MAX) { //move right
stepper1.setSpeed(900);
stepper1.runSpeed();
}
}

This will stop dead when the joystick goes back into the neutral zone.


sn

Dennis

unread,
Apr 4, 2015, 8:02:30 PM4/4/15
to accels...@googlegroups.com
Thanks much Sandy. I took yours and Mike's advice and got it to  work. I appreciate your help.

sandy...@gmail.com

unread,
Apr 5, 2015, 7:47:06 AM4/5/15
to accels...@googlegroups.com
Hurray!

Sent from Windows Mail

John

unread,
Jun 21, 2015, 8:43:17 PM6/21/15
to accels...@googlegroups.com
Dennis, can you please post your final sketch?
Thanks
John

Jon Magill

unread,
Jul 1, 2015, 10:50:26 AM7/1/15
to accels...@googlegroups.com
Bump...

Dennis -- As John asked, can you post your final working version so we can all see what finally worked for your scenario?

Thanks,

--Jon
Reply all
Reply to author
Forward
0 new messages