#include <AccelStepper.h>
#include <Bounce2.h>
#define NUM_BUTTONS 2
const int dirPin = 4;
const int stepPin = 5;
const int motorInterfaceType = 1;
bool start = false;
const int buttonPin[NUM_BUTTONS] = {2, 3};
AccelStepper myStepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};
void setup()
{
Serial.begin(9600);
for (byte i = 0; i < NUM_BUTTONS; i++)
{
button[i].attach(buttonPin[i], INPUT_PULLUP);
button[i].setPressedState(LOW);
button[i].interval(5);
}
myStepper.setMaxSpeed(1000);
}
void loop()
{
if (start == true)
{
myStepper.setSpeed(400);
myStepper.runSpeed();
}
else
{
myStepper.stop();
}
for (byte i = 0; i < NUM_BUTTONS; i++)
{
button[i].update();
if (button[0].pressed() )
{
Serial.println("start");
start = true;
}
else if (button[1].pressed() )
{
Serial.println("stop");
start = false;
}
}
}