Hi everyone,
I’m new to Arduino and been trying to get some code working for making 2 limit switches control 2 stepper motors. I have copied parts of code from other setups but I know I must be doing something wrong!
I run the simulation but the 2 steppers rotate in opposite direction and doesn't change the direction when one of the switches in on.
the Goal is the rotate in same direction and change direction when one of the switches is pressed
here is the Code below:
// C++ code for the upper body control two steppers with two switches and dc motor
// final code for upper body
#include <ezButton.h>
#include <AccelStepper.h>
#define DIRECTION_CCW -1
#define DIRECTION_CW 1
#define STATE_CHANGE_DIR 1
#define STATE_MOVE 2
#define STATE_MOVING 3
#define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type)
ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0;
ezButton limitSwitch_2(A1); // create ezButton object that attach to pin A1;
AccelStepper stepper1(AccelStepper::FULL4WIRE, 7, 6, 5, 4);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 12, 11, 10, 9);
int stepper1State = STATE_MOVE;
int stepper2State = STATE_MOVE;
int direction = DIRECTION_CW;
long targetPos = 0;
void setup() {
Serial.begin(9600);
limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds
stepper1.setMaxSpeed(500.0); // set the maximum speed for Stepper1
stepper1.setAcceleration(50.0); // set acceleration for Stepper1
stepper1.setSpeed(100); // set initial speed for Stepper1
stepper1.setCurrentPosition(0); // set position for Stepper1
stepper2.setMaxSpeed(500.0); // set the maximum speed for Stepper2
stepper2.setAcceleration(50.0); // set acceleration for Stepper2
stepper2.setSpeed(100); // set initial speed for Stepper2
stepper2.setCurrentPosition(0); // set position for Stepper2
}
void loop() {
limitSwitch_1.loop(); // MUST call the loop() function first
limitSwitch_2.loop(); // MUST call the loop() function first
if (limitSwitch_1.isPressed()) {
stepper1State = STATE_CHANGE_DIR;
stepper2State = STATE_CHANGE_DIR;
}
if (limitSwitch_2.isPressed()) {
stepper1State = STATE_CHANGE_DIR;
stepper2State = STATE_CHANGE_DIR;
}
switch (stepper1State) {
case STATE_CHANGE_DIR:
direction *= -1; // change direction
stepper1State = STATE_MOVE; // after changing direction, go to the next state to move the motor
break;
case STATE_MOVE:
targetPos = direction * MAX_POSITION;
stepper1.setCurrentPosition(0); // set position
stepper1.moveTo(targetPos);
stepper1State = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity
break;
case STATE_MOVING: // without this state, the move will stop after reaching maximum position
if (stepper1.distanceToGo() == 0) { // if motor moved to the maximum position
stepper1.setCurrentPosition(0); // reset position to 0
stepper1.moveTo(targetPos); // move the motor to maximum position again
}
break;
}
stepper1.run(); // MUST be called in loop() function
switch (stepper2State) {
case STATE_CHANGE_DIR:
direction *= -1; // change direction
stepper2State = STATE_MOVE; // after changing direction, go to the next state to move the motor
break;
case STATE_MOVE:
targetPos = direction * MAX_POSITION;
stepper2.setCurrentPosition(0); // set position
stepper2.moveTo(targetPos);
stepper2State = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity
break;
case STATE_MOVING: // without this state, the move will stop after reaching maximum position
if (stepper2.distanceToGo() == 0) { // if motor moved to the maximum position
stepper2.setCurrentPosition(0); // reset position to 0
stepper2.moveTo(targetPos); // move the motor to maximum position again
}
break;
}
stepper2.run(); // MUST be called in loop() function
}