#include <AccelStepper.h>
//pins
int Stepper1Pulse = 5;
int Stepper1Direction = 6;
int speed1pot = A2;
int speed2pot = A3;
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
//terms
int low1Limit = 100;
int pot1Val = 0;
int Speed1 = 0;
int speed1min = 0;
int speed1max = 2000;
int pot2Val = 0;
int Speed2 = 0;
int speed2min = 100;
int speed2max = 800;
// Define the AccelStepper interface type:
#define MotorInterfaceType 2
AccelStepper stepper1(1, Stepper1Pulse, Stepper1Direction);
AccelStepper stepper2(MotorInterfaceType, dirA, dirB);
void setup() {
pinMode(Stepper1Pulse, OUTPUT);
pinMode(Stepper1Direction, OUTPUT);
digitalWrite(Stepper1Pulse, LOW);
digitalWrite(Stepper1Direction, LOW);
// Set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
stepper1.setMaxSpeed(speed1max);
stepper1.setSpeed(0);
stepper1.setAcceleration(2000);
stepper1.moveTo(2000);
stepper2.setSpeed(0);
stepper2.setMaxSpeed(speed2max);
stepper2.setAcceleration(20);
}
void loop() {
pot1Val = analogRead(speed1pot); // read the pot
pot2Val = analogRead(speed2pot); // read the pot
if (low1Limit >= pot1Val) { // if less than or equal to lowLimit
if (stepper1.distanceToGo() == 0) { // If at the end of travel go to the other end
stepper1.moveTo(-stepper1.currentPosition());
} else stepper1.run();
} else {
Speed2 = map(pot2Val, 0, 1023, speed2min, speed2max);
stepper2.setSpeed(Speed2);
stepper2.run();
Speed1 = map(pot1Val, 0, 1023, speed1min, speed1max);
stepper1.setSpeed(Speed1);
stepper1.run();
}
}