Make Stepper Run at Constant Speed While Button Depressed

3,829 views
Skip to first unread message

Ray Robertson

unread,
Sep 9, 2016, 5:37:13 PM9/9/16
to accelstepper
I'm having an issue trying to figure out how to get the most speed out of my stepper motor while a button is being depressed.

I have a 3 position toggle switch to choose the direction of motion for the stepper motor to move. I want the stepper motor to stop as soon as possible once the switch is off. I will be adding a limit switch in later as well.

With the code I have, everything works, but the motor isn't running as fast as it can without the "if" statement. I'm assuming it's just the timing in the arduino going through the "if" statement and checking the variables that is slowing it down.

Is there another or better way to implement this that might get me a faster speed? Or can I send it a command to run a few steps while the arduino reprocesses the variable and checks the statements and conditions? I've tried speeds up to 100,000. It runs exactly the same as with 4,000.

#include <AccelStepper.h>

AccelStepper stepper(1, 3, 2);

const int rightB = 7;
const int leftB = 9;

int rightBstate = 0;
int leftBstate = 0;

void setup()
{
 Serial.begin(9600);
 pinMode(rightB, INPUT_PULLUP);
 pinMode(leftB, INPUT_PULLUP);

 stepper.setMaxSpeed(4000);
 stepper.setSpeed(4000);
}

void loop()
{
  rightBstate = digitalRead(rightB);
  leftBstate = digitalRead(leftB);

 if (rightBstate == LOW && leftBstate == HIGH) {
  Serial.println("Right Button");
  stepper.setSpeed(4000);
  stepper.run();
}

if (leftBstate == LOW && rightBstate == HIGH) {
  Serial.println("Left Button");
  stepper.setSpeed(-4000);
  stepper.runSpeed();
}
}

Ray Robertson

unread,
Sep 9, 2016, 7:14:07 PM9/9/16
to accelstepper
Okay, I tried the same code without the Serial. commands, and it worked just fine.

I didn't realize the Serial. commands with driving the stepper would be too much for the Arduino.

Luke Lawcock

unread,
Oct 14, 2016, 10:57:40 AM10/14/16
to accelstepper
Hi,

I am trying to achieve the same thing, run a stepper at a constant speed while a button is depressed. I have copied the code and i too had the issue with very slow speed, i also deleted the serial but now i can not get it to run at the speed i want. It will not read the acceleration I want even though i had added  stepper.setAcceleration(1000);

Any advice

KR Luke

gregor

unread,
Oct 14, 2016, 11:45:34 AM10/14/16
to accelstepper
if you want acceleration, use run(), runSpeed() is to move the stepper motor at a specific, constant speed.
AccelStepper is not really designed for a combination of acceleration and instant stop. I use the following for when endstop switches are hit, this may work for you:

if(instant_stop_btn)
{
    stepper.move(0);
    stepper.setSpeed(0.0f);
}

Luke Lawcock

unread,
Oct 17, 2016, 6:24:33 PM10/17/16
to accelstepper
Hi All,

I have made an attempt to write my own program, however i'm still struggling to get it to do what i want it to do, some help would be greatful.

When I the button it ramps up but if i release the button it will stop but when i press it again it again it continues to ramp from where it was. I need it to start the ramp again from zero, every time the button is pressed and either stop sharply or ramp down when the button is released. 

Here is my code; 

#include <AccelStepper.h>
#include <MultiStepper.h>

AccelStepper motor(1, 7, 8);

const int buttonPin = 3;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  motor.setMaxSpeed(4800);
  motor.setAcceleration(800);
  motor.setMinPulseWidth(100);
  motor.setCurrentPosition(0);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is low:
  if (buttonState == LOW) {
    motor.moveTo(24000);
    motor.run();
  } else {
    motor.move(0);
    motor.setSpeed(0.0f);
  }
}

Kind Regards, Luke

On Friday, 9 September 2016 22:37:13 UTC+1, Ray Robertson wrote:

gregor

unread,
Oct 18, 2016, 2:14:30 AM10/18/16
to accelstepper
That's because AccelStepper is not designed for mixing Acceleration and instant speed changes. 

I think it should work if you use a very high acceleration when stopping the stepper:
if (buttonState == LOW) {
    motor.setAcceleration(800);
    motor.moveTo(24000);
} else {
    motor.setAcceleration(100000000);
    motor.stop();
}

motor.run();

Luke Lawcock

unread,
Oct 18, 2016, 9:39:04 AM10/18/16
to accelstepper
I adjusted the code to the following, however now it runs continuously ramping up and down to 2250 steps but if i press the button, it reverses the amount of steps covered in one go and then stops?

#include <AccelStepper.h>
#include <MultiStepper.h>

AccelStepper motor(1, 7, 8);

const int buttonPin = 3;     // the number of the pushbutton pin

// * pushbutton attached to pin 3 from +5V
// * 10K resistor attached to pin 3 from ground

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  motor.setMaxSpeed(4800);
  motor.setAcceleration(19200);
  motor.setMinPulseWidth(100);
  motor.setCurrentPosition(0);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is high:
if (buttonState == HIGH) {
    motor.setAcceleration(19200);
    motor.moveTo(2250);
} else {
    motor.setAcceleration(19200);
    motor.stop();
}

  motor.run();
}


On Friday, 9 September 2016 22:37:13 UTC+1, Ray Robertson wrote:

Luke Lawcock

unread,
Oct 18, 2016, 9:51:06 AM10/18/16
to accelstepper
Adding motor.setCurrentPosition(0); to the else statement gives me the desired result from the button but in the off position the motor is creeping slowly forward. Any suggestions?
//    motor.setCurrentPosition(0);
    motor.stop();
}

  motor.run();
}


On Friday, 9 September 2016 22:37:13 UTC+1, Ray Robertson wrote:

Luke Lawcock

unread,
Oct 18, 2016, 9:56:14 AM10/18/16
to accelstepper
Having shortened the wires to the button, the creeping is much less often, however, still not acceptable. I have a 10k resistor in circuit, any suggestions?


On Friday, 9 September 2016 22:37:13 UTC+1, Ray Robertson wrote:
Reply all
Reply to author
Forward
0 new messages