I’m setting up 2 stepper motors to trigger on switches, going one direction on one button press, and the opposite direction on the other button’s press. Right now everything in my code works as I want it: locking out of being able to go the opposite direction until the other button is pressed. However, the 2nd motor as of now is not working correctly, on either button press the 2nd motor will continue in the same direction, instead of turning the opposite direction, like the 1st motor properly functions. I don’t understand how my code could be wrong, it’s basically a mirror of the first motor.
#include <Stepper.h>
#define motorSteps 48 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define motorPin5 4
#define motorPin6 5
#define motorPin7 6
#define motorPin8 7
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);
Stepper myStepper2(motorSteps, motorPin8, motorPin5, motorPin6, motorPin7);
const int buttonPin2 = 2;
const int buttonPin3 = 3;
int buttonState1 = 0;
int buttonState2 = 0;
int Start = 0;
int End = 0;
void setup()
{
// set the motor speed at 60 RPMS:
myStepper.setSpeed(60);
myStepper2.setSpeed(60);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
// Initialize the Serial port:
Serial.begin(9600);
}
void loop()
{
buttonState1 = digitalRead(buttonPin2);
buttonState2 = digitalRead(buttonPin3);
if (buttonState1 == HIGH)
while (Start == 0)
{
// Step forward 100 steps:
Serial.println(“Forward”);
myStepper.step(100);
myStepper2.step(100);
Start++;
//int End;
End = 0;
}
if (buttonState2 == HIGH)
while (End == 0)
{
// Step backward 100 steps:
Serial.println(“Backward”);
myStepper.step(-100);
myStepper2.step(-100);
End++;
//int Start;
Start = 0;
}
}