#include <AccelStepper.h>
#define STEP_PIN 2 // Step pin for the stepper motor driver
#define DIR_PIN 3 // Direction pin for the stepper motor driver
#define ENABLE_PIN 4 // Enable pin for the stepper motor driver
#define speedPot A0 // Potentiometer analog input pin
#define directionButton 5 // Direction button pin
#define onOffButton 6 // On/Off button pin
#define leftLimitSwitch 7 // Left limit switch pin
#define rightLimitSwitch 8 // Right limit switch pin
AccelStepper stepper(1, STEP_PIN, DIR_PIN); // 1 = A4988 driver
bool direction = false; // Initial direction set it to true to change direction
bool runMotor = true; // flag to check if user
unsigned long pressTime = 0; // Variable to hold button press time
bool buttonPress = false;// Flag to check if button was pressed or not
int dir = -1; // initial direction set it to 1, -1
long StepperPos = 0; //added STKO!!
unsigned long dirButtonTime = millis();
void setup() {
Serial.begin(9600);
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Disable the stepper initially
pinMode(directionButton, INPUT_PULLUP);
pinMode(onOffButton, INPUT_PULLUP);
pinMode(leftLimitSwitch, INPUT_PULLUP);
pinMode(rightLimitSwitch, INPUT_PULLUP);
stepper.setMaxSpeed(1000); // Set your desired max speed
stepper.setAcceleration(50); // Set your desired acceleration
}
void loop() {
while (digitalRead(onOffButton) == LOW) {
digitalWrite(ENABLE_PIN, LOW); // Enable the stepper
int potValue = analogRead(speedPot);
int speed = map(potValue, 0, 1023, 0, 1000); // Map potentiometer value to speed range
// Check direction button
/*if (digitalRead(directionButton) == LOW) { //if (digitalRead(directionButton) == LOW) {
if ((millis() - dirButtonTime) > 500) { // Debounce button press effect
dir *= -1; // change direction
dirButtonTime = millis();
}
}
*/
//ADDED STKO BEGIN
if (dir == -1) //if (dir == -1 && dtValue == HIGH)
{
Serial.println("Rotated clockwise");
stepper.runToNewPosition(-500);
//speed = speed + 50;
//Nema17.setSpeed(speed);
dir *=-1;
}
if (dir == 1) // if (dir == 1 && dtValue == LOW)
{
Serial.println("Rotated counterclockwise");
stepper.runToNewPosition(500);
//speed = speed - 50;
//Nema17.setSpeed(speed);
dir *=-1;
}
//ADD STKO END
stepper.setSpeed(speed * dir);
// Check limit switches and prevent movement in the pressed direction
if ((digitalRead(leftLimitSwitch) == LOW) && (buttonPress == false)) {
Serial.println(leftLimitSwitch);
dir *= -1; // change direction
buttonPress = true; // Set button press flag
pressTime = millis(); // Note current press time
delay(1000);
stepper.move(500);
}
if ((digitalRead(rightLimitSwitch) == LOW) && (buttonPress == false)) {
Serial.println(rightLimitSwitch);
dir *= -1; // change direction
buttonPress = true; // Set button press flag
pressTime = millis(); // Note current press time
delay(1000);
stepper.move(-500);
}
if (((millis() - pressTime) > 1000) && (buttonPress)) {
buttonPress = false; // Reset button press flag
}
stepper.run();
}
digitalWrite(ENABLE_PIN, HIGH); // Enable the stepper
}