#include <AccelStepper.h>
// Define pin assignments
#define dirPin 8
#define stepPin 9
#define switchPin 6
#define switchOutPin 2
// Define stepper motor parameters
const int stepsPerRevolution = 200; // Change based on stepper motor specs
const float degreesPerStep = 360.0 / stepsPerRevolution;
const int stepsPer90Degrees = stepsPerRevolution / 4; // Steps for 90 degrees
// Define rotation metrics
const float maxSpeed = 30; // Max speed (steps per second)
const float setAcceleration = 1; // Acceleration (steps per second^2)
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
pinMode(switchPin, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(switchOutPin, OUTPUT);
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(setAcceleration);
stepper.setCurrentPosition(0);
}
void loop() {
if(digitalRead(switchPin) == HIGH) {
digitalWrite(LED_BUILTIN, digitalRead(switchPin));
stepper.moveTo(50);
stepper.runToPosition();
digitalWrite(switchOutPin,digitalRead(switchPin));
}
else if (digitalRead(switchPin) == LOW) {
digitalWrite(LED_BUILTIN, digitalRead(switchPin));
stepper.moveTo(0);
stepper.runToPosition();
digitalWrite(switchOutPin,digitalRead(switchPin));
}
}