#include "ESP_FlexyStepper.h"
// IO pin assignments
const int MOTOR_STEP_PIN = 15;
const int MOTOR_DIRECTION_PIN = 2;
const int EMERGENCY_STOP_PIN = 33; //define the IO pin the emergency stop switch is connected to
const int LIMIT_SWITCH_PIN = 32; //define the IO pin where the limit switches are connected to (switches in series in normally closed setup against ground)
// 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 = 10;
const int DECELERATION_IN_STEPS_PER_SECOND = 10;
// create the stepper motor object
ESP_FlexyStepper stepper;
void IRAM_ATTR onTimer() {
// Call your function from the interrupt handler
//ets_printf("interrupt");
stepper.processMovement();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.printf("Heap %d\n", ESP.getFreeHeap());
// 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 regularily so you can do whatever you want in the loop function
//stepper.startAsService();
stepper.setTargetPositionInSteps(1000);
//Serial.printf("myvar %d\n", myvar);
Serial.printf("Heap %d\n", ESP.getFreeHeap());
// Configure and start a hardware timer
hw_timer_t *timer = NULL;
timer = timerBegin(0, 80, true); // Timer 0, 80 prescaler, count up
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 1 * 1000, true); // Convert interval to microseconds
timerAlarmEnable(timer);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}