/*
Arduino | coding-help
Seth
Friday, November 7, 2025 8:55 PM
I'm struggling to get commands in from serial monitor to move a stepper.
*/
#include <AccelStepper.h>
// AccelStepper Setup
AccelStepper theta1 = AccelStepper(AccelStepper::DRIVER, 24, 22);
// Define the Pins used
#define home_switch 10 // Pin 10 connected to Home Switch (MicroSwitch)
// Stepper Travel Variables
int TravelX; // Used to store the X value entered in the Serial Monitor
bool move_finished = false; // Used to check if move is completed
int initial_homing = -1;
void setup() {
Serial.begin(115200);
pinMode(home_switch, INPUT_PULLUP); // uses internal pullup resistor
// Set Max Speed and Acceleration of each Steppers at startup for homing
theta1.setMaxSpeed(50.0); // Set Max Speed of Stepper (Slower to get better accuracy)
theta1.setAcceleration(50.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
Serial.println("Stepper is Homing");
Serial.println(digitalRead(home_switch));
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
theta1.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
theta1.run(); // Start moving the stepper
}
theta1.setCurrentPosition(0); // Set the current position as zero for now
theta1.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
theta1.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
delay(5);
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
theta1.moveTo(initial_homing);
theta1.run();
initial_homing++;
delay(5);
}
move_finished = true;
theta1.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
theta1.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
theta1.setAcceleration(1000.0); // Set Acceleration of Stepper
// Print out Instructions on the Serial Monitor at Start
Serial.println("Setup: Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
// delay(10000);
}
void loop() {
while (Serial.available() > 0) { // Check if values are available in the Serial Buffer
TravelX = Serial.parseInt(); // Put numeric value from buffer in TravelX variable
Serial.print("Moving stepper into position: ");
Serial.println(TravelX);
theta1.moveTo(TravelX); // Set new moveto position of Stepper
Serial.print("distance.toGo(TravelX) = ");
Serial.println(theta1.distanceToGo());
break;
// delay(1000); // Wait 1 seconds before moving the Stepper
}
if ((theta1.distanceToGo() != 0)) { // Check if the Stepper has reached desired position
theta1.run(); // Move Stepper into position
Serial.println("statement 2");
}
// If move is completed display message on Serial Monitor
if (theta1.distanceToGo() == 0) {
Serial.println("COMPLETED!");
Serial.println("");
Serial.println("Loop: Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
delay(10000);
}
}
Home