#include <Stepper.h>
const int stepsPerRevolution = 200; // Number of steps per revolution for your motor
Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);
#define LimitBtn 9
unsigned long debounceDuration = 50; // millis
void setup() {
pinMode(LimitBtn, INPUT_PULLUP);
myStepper.setSpeed(10); // Set motor speed to 10 RPM
Serial.begin(9600);
int stepCount = 0; // Initialize step count
// Find home position at startup
Serial.println("Finding home position...");
while (digitalRead(LimitBtn) == HIGH) {
// Move until the limit switch is touched (LOW)
myStepper.step(-1); // Move step by step in a direction to find home
stepCount--; // Decrease step count for each step taken
delay(10); // Small delay between steps
}
// Stop motor once home position is found
Serial.println("Home position found!");
// If you want to move the motor back to the original position, do this:
Serial.println("Returning to original position...");
myStepper.step(-stepCount); // Move back the same number of steps
Serial.println("Returned to original position.");
}
void loop() {
// Additional logic can be implemented here
}