How to "reset" speed back to zero?

667 views
Skip to first unread message

Chris Chimienti

unread,
May 2, 2013, 11:40:58 AM5/2/13
to accels...@googlegroups.com
I'm trying to use the AccelStepper library to smoothly operate my stepper motor (with Arduino). I have a simple program right now that basically moves the stepper in one direction while one button is pushed, and then the other direction when the other button is pushed.

However, the code I've written so far does that (sort of). The problem I'm having is that once the motor accelerates in one direction (ex: clockwise) and I let go of the button (and the stepper is now stopped), it seems to "remember" that speed and acceleration in that direction. When I go to press the button to travel in the opposite direction (counter-clockwise) the stepper will resume it's previous speed clockwise direction and deccelerate until the speed is zero and THEN move in the counter clockwise direction.

I would like it such that every time the button is released, the speed is set back to zero. I cannot seem to find the function in the library to do this?

Here is my code:
#include <AccelStepper.h>

AccelStepper motor(1,7,8); // step pin = 7 dir pin = 8
int uppin = 3; //pin number for the up button
int downpin = 4; //pin number for the down button

void setup() {


motor.setMaxSpeed(4000);
motor.setAcceleration(1500);
motor.setMinPulseWidth(100);
motor.setCurrentPosition(0);

pinMode(uppin, INPUT);
pinMode(downpin, INPUT);

Serial.begin(9600);

}

void loop() {

if (digitalRead(uppin) == HIGH) { // check if the input is HIGH (button pushed)
motor.moveTo(75000); //Set target (absolute position)
moveup(); // run the moveup subroutine
}

if (digitalRead(downpin) == HIGH) { // check if the input is HIGH (button pushed)
motor.moveTo(0); //Set target (absolute position)
movedown(); // run the movedown subroutine
}


Serial.println(motor.currentPosition());
}

void moveup() {
//routine to move the platform up

while (digitalRead(uppin) == HIGH) {

if (motor.currentPosition() != 75000) {

motor.run();
}

}

}


void movedown() {
//routine to move the platform down

while (digitalRead(downpin) == HIGH) {

if (motor.currentPosition() != 0) {

motor.run();
}
}

}

Sandy Noble

unread,
May 2, 2013, 4:55:15 PM5/2/13
to accels...@googlegroups.com
Hi!  I think the solution is just to do

  motor.setSpeed(0);
  motor.moveTo(motor.currentPosition);

every time the button is released.  The reason why your speed continued before is because although you were not run()ing the motor, it had not decelerated before you gave it the new target to head towards.

The main issue though is that you are not looking for the button being released, only the button being held down.

My version would be something like:

boolean buttonWasPressed = false;

void loop() {

if (digitalRead(uppin) == HIGH) { // check if the input is HIGH (button pushed)
            buttonWasPressed = true;
    motor.move(10000); //Set target (relative position)
}
else if (digitalRead(downpin) == HIGH) { // check if the input is HIGH (button pushed)
           buttonWasPressed = true;
   motor.move(-10000); //Set target (absolute position)
        else if (buttonWasPressed)
        {
           // button was pressed, but isn't now!  Lets halt!!
           motor.setSpeed(0);
           motor.moveTo(motor.currentPosition);
           // an alternative is motor.stop() that will smoothly decelerate to a halt.
        }

        motor.run(); // will only step if required

        Serial.println(motor.currentPosition());
}


Though note that Serial.println(...) will slow your motors down a lot.  The .move(10000) assumes that 10000 steps is enough to get to top speed, but not enough to start decelerating.

I haven't tested the above.  You might choose to use a buttons library for debouncing your buttons.


--
sn

Chris Chimienti

unread,
May 2, 2013, 10:39:52 PM5/2/13
to accels...@googlegroups.com
Your first suggestion worked wonderfully, thank you!
Reply all
Reply to author
Forward
0 new messages