Go Down

Topic: Accelstepper move while button pressed (Read 115 times) previous topic - next topic

flotchino

Hi, this code works fine for stepper movement. When button is briefly pressed, it moves the set amount with smooth ramp up and down curves.
But I wish to be able to hold button down and have it ramp up and keep motor at set speed until letting go of button which will ramp down motor to a stop. I've experimented and searched everywhere but no luck as yet.
Thanks if anyone can help.

Code: [Select]
#include <AccelStepper.h>
AccelStepper stepper(1,7,8);
int buttonPin = 2;
int buttonState = LOW;
 
void setup()

stepper.setMaxSpeed(500);
stepper.setAcceleration(200);
stepper.setMinPulseWidth(100);
}
 
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
if (stepper.distanceToGo() == 0)
 
stepper.move(500);
stepper.run();
}

MarkT

You'll have to start thinking in terms of a state-machine - if this is unfamiliar now is the time
to learn...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Robin2

To follow what @MarkT has said, you need to do things when the state of the button changes.

For example start the ramp-up when it goes from not-pressed to pressed and start the ramp-down when it goes from pressed to not-pressed.

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

flotchino

Thanks for your advice. This code will do the job but has a strange acceleration curve (seems to be somewhat incremental) but the de-acceleration is fine. It would be nice to have it working properly.
Any ideas as to what I've done wrong?

Thanks again.
Code: [Select]
#include <AccelStepper.h>
AccelStepper stepper(1,7,8);
int buttonPin = 2;
int buttonState;
 
void setup()

stepper.setMaxSpeed(2000);
stepper.setAcceleration(200);
stepper.setMinPulseWidth(100);
pinMode(buttonPin, INPUT);
}
 
void loop()
{
    buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH)
    stepper. move (20000);
    stepper. run ();
   
    buttonState = digitalRead(buttonPin);
 if (buttonState == LOW)

{
    stepper.stop();
    stepper.runToPosition();
}
}

Robin2

Any ideas as to what I've done wrong?
You don't seem to have taken any account of the advice.

As written your code acts WHEN the buttonState is HIGH. It should be working when it CHANGES from LOW to HIGH.

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

flotchino

Still learning, thanks for that powerful message.
I will keep trying to understand.

MarkT

To detect change you must record the previous state so you can compare with the current state.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy