#include <AccelStepper.h>
// Define pin assignments
#define dirPin 8
#define stepPin 9
#define switchPin 6
const float maxSpeed = 1350; // Max speed (steps per second)
const float setAcceleration = 450; // Acceleration (steps per second^2)
int currentSwitchState = 0; // current state of the Switch
int lastSwitchState = LOW;
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
pinMode(switchPin, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(setAcceleration);
}
void loop() {
// Read Switch state
currentSwitchState = digitalRead(switchPin);
if (currentSwitchState != lastSwitchState) {
if (currentSwitchState == HIGH && lastSwitchState == LOW) {
digitalWrite(LED_BUILTIN, HIGH);
//Serial.println("Rotating 90 degrees clockwise");
stepper.moveTo(16200);
}
if (currentSwitchState == LOW && lastSwitchState == HIGH) {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Rotating 90 degrees anti-clockwise");
stepper.moveTo(0);
}
stepper.runToPosition();
//Update last switch state
lastSwitchState = currentSwitchState;
}
}