#include <AccelStepper.h>
// defines pins numbers
const int stepPin = 7;
const int directionPin = 6;
AccelStepper stepper(AccelStepper::DRIVER, stepPin, directionPin);
float strokeLength = 169;
int speed = 100;
long accel = 100000;
int startPosition = 49;
float maxPosition = 600;
int power = 12;
int setMax = 1;
int home = 1;
int homing = -1;
void setup() {
Serial.begin(9600); //Starts serial coms
pinMode(A0, INPUT); //Speed pot defined
pinMode(A1, INPUT); //Stroke pot length defined
pinMode(A2, INPUT); //Max pot position define
pinMode(12, INPUT); //start/stop switch
pinMode(11, INPUT); //set max position withoutstroke
pinMode(10, INPUT); //Homing pin
home = digitalRead(10);
while (home == LOW){
stepper.setMaxSpeed(50); //Homing speed
stepper.setAcceleration(accel); //acceleration set
stepper.runToNewPosition(homing); // moves back one step
home = digitalRead(10); //reads for completely retracted
stepper.setCurrentPosition(0); //set current position to 0
delay(100); // slows down the retraction
}
stepper.setMaxSpeed(speed); //initial speed set
stepper.setAcceleration(accel); //acceleration set
stepper.runToNewPosition(startPosition); //Initial start potition set
}
void loop() {
//Used for setting the max poisition without machine running
setMax = digitalRead(11); //Reads for max position switch
while (setMax == HIGH){ //validates switch is on
maxPosition = analogRead(A2); //reads max pot position
maxPosition = startPosition + map(maxPosition,0,1023,49,882); //Scales max pot
Serial.print("Max Position - ");
Serial.println(maxPosition/49);
stepper.runToNewPosition(maxPosition); //Moves to max position
setMax = digitalRead(11); //re-reads for max position switch
delay (10); //reasons.... IDK
}
//Standard operation loop
power = digitalRead(12); //Reads for power switch
if (power == HIGH){ //validates power switch is on
//Sets stroke length
strokeLength = analogRead(A1); // reads the stroke pot
strokeLength = map(strokeLength,0,1023,49,882); //Scales storke pot to 0-1023
Serial.print("Stroke - "); // Prints stoke length
Serial.println(strokeLength/49); //Prints the scalled stroke length
//sets max position
maxPosition = analogRead(A2); //reads max pot position
maxPosition = startPosition + map(maxPosition,0,1023,49,882); //Scales max pot
Serial.print("Max Position - "); // Prints max position
Serial.println(maxPosition/49); // Prints max position
//set speed and moves
speed = analogRead(A0); // Reads the speed pot
speed = map(speed,0,1023,50,1000); //Scales the pot to 0-1023
Serial.print("Speed - "); //Prints the scalled pot speed 0-1023
Serial.println(speed); //Prints the scalled pot speed 0-1023
stepper.setMaxSpeed(speed); // sets the motor speed
stepper.runToNewPosition(maxPosition); //Moves motor to the max position
speed = analogRead(A0); // re-reads the speed pot
speed = map(speed,0,1023,50,1000); //re-scales the pot to 0-1023
stepper.setMaxSpeed(speed); //re-sets the motor speed
if (maxPosition - strokeLength < startPosition){ //safety to keep stroke bottoming out
strokeLength = maxPosition - startPosition; //Sets stroke length to stop at min position
}
stepper.runToNewPosition(maxPosition - strokeLength); //moves motor to min stroke position
}
}