#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// https://wokwi.com/projects/288681423014986248
const int dirPin1 = 12;
const int stepPin1 = 14;
const int home_switch_pin = 27; // Pin Home Switch (MicroSwitch)
// AccelStepper Setup
AccelStepper stepperX(AccelStepper::DRIVER, stepPin1, dirPin1);
// Stepper Travel Variables
bool homing_complete = false;
int initial_homing = 1;
int buttonValue = digitalRead((home_switch_pin));
int lastState = HIGH;
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
// pinMode(home_switch, INPUT_PULLUP);
// int value = digitalRead((BUTTON_PIN));
delay(5); // Wait for EasyDriver wake up
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepperX.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(100.0); // Set Acceleration of Stepper
// homing();
}
void loop() {
if (lastState != buttonValue) {
lastState = buttonValue;
if (buttonValue == HIGH) {
Serial.println(" released");
}
if (buttonValue == LOW) {
Serial.println(" pressed");
}
}
// Serial.print("Stepper is Homing . . . . . . . . . . . ");
// if (homing_complete) {
// Serial.println("button TRUE . . . . . . . . . . . ");
// } else {
// Serial.println("button FALSE . . . . . . . . . . . ");
// }
}
void homing() {
// Start Homing procedure of Stepper Motor at startup
Serial.print("Stepper is Homing . . . . . . . . . . . ");
if (homing_complete) {
Serial.println("button TRUE . . . . . . . . . . . ");
} else {
Serial.println("button FALSE . . . . . . . . . . . ");
}
// while (digitalRead(home_switch) && !homing_complete) { // Make the Stepper move CCW until the switch is activated
// Serial.print("button active . . . . . . . . . . . ");
// stepperX.moveTo(initial_homing); // Set the position to move to
// initial_homing--; // Decrease by 1 for next move if needed
// stepperX.run(); // Start moving the stepper
// delay(5);
// }
// stepperX.setCurrentPosition(0); // Set the current position as zero for now
// stepperX.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
// stepperX.setAcceleration(100.0); // Set Acceleration of Stepper
// initial_homing = 1;
// stepperX.moveTo(100);
// stepperX.run();
// while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
// stepperX.moveTo(initial_homing);
// stepperX.run();
// initial_homing++;
// delay(5);
// }
// stepperX.setCurrentPosition(0);
// Serial.println("Homing Completed");
// Serial.println("");
// stepperX.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
// stepperX.setAcceleration(1000.0); // Set Acceleration of Stepper
}