#include <AccelStepper.h>
int maxSteps = 200;
int stepPin = 3;
int dirPin = 2;
const int maxSpeed = 1000;
const int acceleration = 1000;
AccelStepper stepper1(1, stepPin, dirPin);
struct motorStructure{
bool main;
bool start;
int speed;
int position;
};
motorStructure motor1;
void setup() {
Serial.begin(9600);
motorInit(stepper1);
motor1 = {true, true, 1, 5};
}
void loop() {
motor1 = function(stepper1, motor1);
motor1.position = -5;
motor1.speed = 5;
}
motorStructure function(AccelStepper &stepperMotor, struct motorStructure x){
bool main = x.main;
bool start = x.start;
int speed = x.speed;
int position = x.position;
// Only Execute if 'main' boolean is triggered
if(main){
// Start First Motion as its own Action
if(start){
stepperMotor.moveTo(position);
stepperMotor.setSpeed(speed);
stepperMotor.runSpeedToPosition();
x.start = false;
}
else{
// Check to See if Motor is at End of Motion Before Re-Assigning New Position
// [non-blocking workaround]
if(stepperMotor.distanceToGo() == 0){
stepperMotor.moveTo(position);
stepperMotor.setSpeed(speed);
}
stepperMotor.runSpeedToPosition();
}
}
return x;
}
void motorInit(AccelStepper &stepperMotor){
stepperMotor.setMaxSpeed(maxSpeed);
stepperMotor.setAcceleration(acceleration);
}
// motorStructure function(AccelStepper &stepperMotor, struct motorStructure x){
// bool start = x.start;
// bool direction = x.direction;
// if(start){
// int directionSign = plusMinus(direction);
// stepperMotor.setSpeed(speed);
// stepperMotor.moveTo(directionSign * waveSteps);
// stepperMotor.runSpeedToPosition();
// x.start = false;
// }
// else{
// if(stepperMotor.distanceToGo() == 0){
// direction = !direction;
// x.direction = direction;
// int directionSign = plusMinus(direction);
// stepperMotor.moveTo(directionSign * waveSteps);
// }
// stepperMotor.setSpeed(speed);
// stepperMotor.runSpeedToPosition();
// }
// return x;
// }
// int plusMinus(bool direction){
// if(direction){
// return +1;
// }
// else{
// return -1;
// }
// }