/*
* This Arduino Nano ESP32 code was developed by newbiely.com
*
* This Arduino Nano ESP32 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-28byj-48-stepper-motor-uln2003-driver
*/
// Include the AccelStepper Library
#include <AccelStepper.h>
#define STEP_PER_REVOLUTION 100 // 100=1024
const int modeSw = 12;
const int limitSw = 13;
int analogVal = 0;
int limitSwVal = 1;
bool limitSwFc = false;
int spdvar = 50;
int dstvar = STEP_PER_REVOLUTION/4;
// The Arduino Nano ESP32 pin entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper stepper(AccelStepper::FULL4WIRE, 11, 9, 10, 8);
void setup() {
pinMode(modeSw, INPUT_PULLUP);
pinMode(limitSw, INPUT_PULLUP);
Serial.begin(9600);
stepper.setMaxSpeed(3000.0); // set the maximum speed
stepper.setAcceleration(1000.0); // set acceleration
CalibCycle();
}
void loop() {
}
void CalibCycle() {
Serial.println("Searching limit...");
while (limitSwVal == 1){
stepper.setSpeed(30);
stepper.runSpeed();
limitSwVal = digitalRead(limitSw);
}
if ((limitSwVal == 0)&&(limitSwFc==false)){
stepper.setCurrentPosition(STEP_PER_REVOLUTION+10);
while (stepper.currentPosition() != STEP_PER_REVOLUTION){
stepper.setMaxSpeed(100);
stepper.runToNewPosition(STEP_PER_REVOLUTION);
Serial.println("Limit reached");
}
limitSwFc=true;
Serial.println("Calibration done");
//===================================Set after calibration settings==================
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(3000.0);
stepper.setAcceleration(1000.0);
}
}