td75:
Only one stepper will move at a time - they both need to move simultaneously if limit switches are activated. Also when the stepper moves it's very slow/jerky.
Try this version
//Supply Reel DVR8825 Driver Pins (A)
byte stepPinA = 2; // Define pin 2 as the steps pin
byte dirPinA = 3; // Define pin 3 as the direction pin
byte limitSwitchPinA = 4; // Define pin 4 for Limit Switch 1
byte resetPinA = 5; // Pin 5 connected to RESET pin
byte M0A = 6; // Define pin 6 as "M0"
byte M1A = 7; // Define pin 7 as "M1"
byte M2A = 8; // Define pin 8 as "M2"
byte rewindSwitchPinA = 9; // Define pin 9 for Switch for rewind
//Take-up Reel DVR8825 Driver Pins (B)
byte stepPinB = 10; // Define pin 3 as the steps pin
byte dirPinB = 11; // Define pin 4 as the direction pin
byte limitSwitchPinB = 12; // Define pin 12 for limit switch 2
byte resetPinB = 13; // Define pin 13 connected to reset pin
byte M0B = 14; // Define pin 14 as "M0"
byte M1B = 15; // Define pin 15 as "M1"
byte M2B = 16; // Define pin 16 as "M2"
boolean rewindSwitchAPressed = false;
boolean limitSwitchApressed = false;
boolean limitSwitchBpressed = false;
unsigned long curMillis;
unsigned long prevStepMillisA = 0;
unsigned long prevStepMillisB = 0;
unsigned long millisBetweenSteps = 25; // milliseconds
void setup() {
//Supply Reel Setup
pinMode(stepPinA, OUTPUT); // Configures "STEP" as output
pinMode(dirPinA, OUTPUT); // Configures "DIR" as output
pinMode(limitSwitchPinA, INPUT); // Configures "limit switch" as input
pinMode(resetPinA, OUTPUT); // Configures reset pin as output
pinMode(M0A, OUTPUT); // Configures "M0" as output
pinMode(M1A, OUTPUT); // Configures "M1" as output
pinMode(M2A, OUTPUT); // Configures "M2" as output
pinMode(rewindSwitchPinA, INPUT); // Configures "rewind switch" as input
//Take-up Reel Setup
pinMode(stepPinB, OUTPUT); // Configures "STEP" as output
pinMode(dirPinB, OUTPUT); // Configures "DIR" as output
pinMode(limitSwitchPinB, INPUT); // Configures "limit switch" as input
pinMode(resetPinB, OUTPUT); // Configures reset pin as output
pinMode(M0B, OUTPUT); // Configures "M0" as output
pinMode(M1B, OUTPUT); // Configures "M1" as output
pinMode(M2B, OUTPUT); // Configures "M2" as output
digitalWrite(resetPinA, HIGH); // Turn on driver A
digitalWrite(M0A, HIGH); // Configures the steps division (1/32 step)
digitalWrite(M1A, HIGH); // As above
digitalWrite(M2A, HIGH); // As above
digitalWrite(resetPinB, HIGH); // Turn on driver B
digitalWrite(M0B, HIGH); // Configures the steps division (1/32 step)
digitalWrite(M1B, HIGH); // As above
digitalWrite(M2B, HIGH); // As above
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons() {
rewindSwitchAPressed = false;
limitSwitchApressed = false;
limitSwitchBpressed = false;
if (digitalRead(rewindSwitchPinA) == HIGH) {
rewindSwitchAPressed = true;
}
if (digitalRead(limitSwitchPinA) == HIGH) {
limitSwitchApressed = true;
}
if (digitalRead(limitSwitchPinB) == HIGH) {
limitSwitchBpressed = true;
}
}
void actOnButtons() {
//~ if (rewindSwitchAPressed == true && limitSwitchApressed == false && limitSwitchBpressed == false) {
//~ digitalWrite(resetPinA, HIGH); // Turn on driver A
//~ digitalWrite(dirPinA, HIGH); // Rotate clockwise
//~ digitalWrite(M0A, LOW); // Configures the steps division (Full step)
//~ digitalWrite(M1A, LOW); // As above
//~ digitalWrite(M2A, LOW); // As above
//~ singleStepA();
//~ }
if (limitSwitchApressed == true) {
digitalWrite(dirPinA, LOW); // Rotate anticlockwise
singleStepA();
}
if (limitSwitchBpressed == true) {
digitalWrite(dirPinB, LOW); // Rotate anticlockwise
singleStepB();
}
}
void singleStepA() {
if (curMillis - prevStepMillisA >= millisBetweenSteps) {
prevStepMillisA = curMillis;
digitalWrite(stepPinA, HIGH);
digitalWrite(stepPinA, LOW);
}
}
void singleStepB() {
if (curMillis - prevStepMillisB >= millisBetweenSteps) {
prevStepMillisB = curMillis;
digitalWrite(stepPinB, HIGH);
digitalWrite(stepPinB, LOW);
}
}
I have moved some of the code into setup() but the main change I have made is to create separate prevStepMillis variable for each motor.
Separately, I find it very strange that you want the motors to move when the limit switches are pressed. In most cases the purpose of a limit switch is to stop a motor - and maybe signal the need for a change of direction.
...R