The MobaTools stepper library makes that pretty easy. I think that MobaTools stepper is easier to learn and use than Accelstepper. MobaTools docs.
#include <MobaTools.h>
const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;
const byte fwdButtonPin = 9;
const byte backButtonPin = 10;
const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;
MoToStepper stepper( STEPS_REVOLUTION, STEPDIR );
void setup()
{
Serial.begin(115200);
pinMode(fwdButtonPin, INPUT_PULLUP);
pinMode(backButtonPin, INPUT_PULLUP);
stepper.attach( stepPin, dirPin );
stepper.setSpeed(1000); // rpm /10, 1000 = 100 RPM
stepper.setRampLen(100);
stepper.setZero();
}
void loop()
{
bool fwdButtonState = digitalRead(fwdButtonPin);
bool backButtonState = digitalRead(backButtonPin);
if (fwdButtonState == LOW && backButtonState == LOW) // both pushed
{
stepper.rotate(0);
}
else if (fwdButtonState == LOW && backButtonState == HIGH) // fwd pushed
{
stepper.rotate(1); // constant rotate CW
}
else if (fwdButtonState == HIGH && backButtonState == LOW) // back pushed
{
stepper.rotate(-1); // constant rotate CCW
}
else
{
stepper.rotate(0);
}
}
The switches are wired to ground and an input set to pinMode INPUT_PULLUP.
The MobaTools library is available for installation via the IDE library manager.