#include <ESP_FlexyStepper.h>
// IO pin assignments
const int MOTOR_STEP_PIN = 26;
const int MOTOR_DIRECTION_PIN = 27;
const int START_BUTTON=33;
//const int EMERGENCY_STOP_PIN = 13; //急停
//const int LIMIT_SWITCH_PIN = 32; //行程开关
// Speed settings
const int DISTANCE_TO_TRAVEL_IN_STEPS = 2000;
const int SPEED_IN_STEPS_PER_SECOND = 300;
const int ACCELERATION_IN_STEPS_PER_SECOND = 800;
const int DECELERATION_IN_STEPS_PER_SECOND = 800;
// create the stepper motor object
ESP_FlexyStepper stepper;
int previousDirection = 1;
void setup()
{
Serial.begin(115200);
// connect and configure the stepper motor to its IO pins
stepper.connectToPins(MOTOR_STEP_PIN, MOTOR_DIRECTION_PIN);
// set the speed and acceleration rates for the stepper motor
stepper.setSpeedInStepsPerSecond(SPEED_IN_STEPS_PER_SECOND);
stepper.setAccelerationInStepsPerSecondPerSecond(ACCELERATION_IN_STEPS_PER_SECOND);
stepper.setDecelerationInStepsPerSecondPerSecond(DECELERATION_IN_STEPS_PER_SECOND);
// Not start the stepper instance as a service in the "background" as a separate task
// and the OS of the ESP will take care of invoking the processMovement() task regularly so you can do whatever you want in the loop function
stepper.startAsService(1);
pinMode(START_BUTTON, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(START_BUTTON)==LOW){
delay(50);
if (digitalRead(START_BUTTON)==LOW){
if(stepper.motionComplete()==true){
stepper.setTargetPositionRelativeInSteps(100);//正走100步
stepper.setTargetPositionRelativeInSteps(-50);//反走50步
}
else{
stepper.setCurrentPositionAsHomeAndStop();
}
}
}
}