// Include the AccelStepper library: #include //Define Buttons int homeStepperMotorsPin=A1; //Define Stepper Motor Step and Direction Pins #define stepPinLeftStepper 2 //Set stepPinLeftStepper equal to 2 #define dirPinLeftStepper 3 //Set dirPinLeftStepper equal to 3 #define stepPinRightStepper 4 //Set stepPinRightStepper equal to 4 #define dirPinRightStepper 5 //Set dirPinRightStepper equal to 5 #define motorInterfaceType 1 //Set motorInterfaceType equal to 1 (This should always be 1 when using a driver) //Define Limit Switch Pins #define leftStepperHomingSwitch 6 //Set leftStepperHomingSwitch equal to 6 #define rightStepperHomingSwitch 7 //Set rightStepperHomingSwitch to 7 //AccelStepper Setup AccelStepper leftStepper = AccelStepper(motorInterfaceType, stepPinLeftStepper, dirPinLeftStepper); //1 = DRV8825 Driver Interface //Arduino Pin 2 connected to STEP pin of DRV8825 //Arduino Pin 3 connected to DIR pin of DRV8825 AccelStepper rightStepper = AccelStepper(motorInterfaceType, stepPinRightStepper, dirPinRightStepper); //1 = DRV8825 Driver Interface //Arduino Pin 4 connected to STEP pin of DRV8825 //Arduino Pin 5 connected to DIR pin of DRV8825 //int TravelX; long initialHomingLeftStepper = 1; //Set Left Stepper to Move Counter-Clockwise (CCW) once it starts rotating long initialHomingRightStepper = 1; //Set Right Stepper to Move Clockwise (CW) once it starts rotating void setup() { Serial.begin(9600); //Start the serial monitor with a speed of 9600 bauds delay(5); //wait for DRV8825 to wake up //Set Buttons as Input pinMode(homeStepperMotorsPin, INPUT); //Set Buttons to HIGH digitalWrite(homeStepperMotorsPin, HIGH); } void loop() { //Home Stepper Motors (Button A1 - Touch Button) if(digitalRead(homeStepperMotorsPin)==LOW){ //-------------------------------------------------------------------------------- START - Homing Steppers - DO NOT EDIT --------------------------------------------------------------------\\ //-------------------------------------- Left Stepper --------------------------------------\\ //Set Switches as INPUTS pinMode(leftStepperHomingSwitch, INPUT_PULLUP); //Set left stepper homing switch as an input //Set Max Speed and Acceleration of Steppers at Startup for Homing Until Limit Switch is Activated leftStepper.setMaxSpeed(3800); //Set Max Speed of Left Stepper (Slower to get better accuracy) - Do not set too high to prevent clamp from crashing into limit switch leftStepper.setAcceleration(100); //Set Max Acceleration of Left Stepper //Start Homing Procedure of Left Stepper Motor Serial.println("Left Stepper is Homing"); while(digitalRead(leftStepperHomingSwitch)) { //Set Left Stepper to do code in following while loop until the left limit switch is activated leftStepper.moveTo(-initialHomingLeftStepper); //Stepper will move right leftStepper.run(); //Starts Moving the Left Stepper initialHomingLeftStepper--; //Keeps Left Stepper Rotating delay(5); //Slows Down While Loop so Arduino does not have to process so much data } //Left Limt Switch is Now Activated - slow down left stepper and move in other direction until switch is no longer activated leftStepper.setCurrentPosition(0); //Reset position to 0 so when Left Stepper continues moving, it rotates in Clockwise (CW) direction and does not continue in CCW direction leftStepper.setMaxSpeed(100); //Set Max Speed of Left Stepper - Keep this at 100 or less for maximum accuracy leftStepper.setAcceleration(100); //Set Max Acceleration of Left Stepper initialHomingLeftStepper=1; //Set Left Stepper to Move left once it starts rotating while(!digitalRead(leftStepperHomingSwitch)) { //Set Left Stepper to do code in following while loop until the left limit switch is deactivated leftStepper.moveTo(-initialHomingLeftStepper); //Stepper will move left leftStepper.run(); //Starts Moving the Left Stepper initialHomingLeftStepper++; //Keeps Left Stepper Rotating delay(5); //Slows Down While Loop so Arduino does not have to process so much data } leftStepper.setCurrentPosition(0); //The Left Stepper is now set to an initial position of 0 and is ready for the rest of the program to run Serial.println("Left Stepper - Homing Complete"); //Print "Left Stepper - Homing Complete" //-------------------------------------- Right Stepper --------------------------------------\\ //Set Switches as INPUTS pinMode(rightStepperHomingSwitch, INPUT_PULLUP); //Set right stepper homing switch as an input //Set Max Speed and Acceleration of Steppers at Startup for Homing Until Limit Switch is Activated rightStepper.setMaxSpeed(3800); //Set Max Speed of Right Stepper (Slower to get better accuracy) - Do not set too high to prevent clamp from crashing into limit switch rightStepper.setAcceleration(100); //Set Max Acceleration of Right Stepper //Start Homing Procedure of Right Stepper Motor Serial.println("Right Stepper is Homing"); while(digitalRead(rightStepperHomingSwitch)) { //Set Right Stepper to do code in following while loop until the left limit switch is activated rightStepper.moveTo(-initialHomingRightStepper); //Stepper will move left rightStepper.run(); //Starts Moving the Right Stepper initialHomingRightStepper--; //Keeps Right Stepper Rotating delay(5); //Slows Down While Loop so Arduino does not have to process so much data } //Right Limt Switch is Now Activated - slow down right stepper and move in other direction until switch is no longer activated rightStepper.setCurrentPosition(0); //Reset position to 0 so when Right Stepper continues moving, it rotates in Clockwise (CW) direction and does not continue in CCW direction rightStepper.setMaxSpeed(100); //Set Max Speed of Right Stepper - Keep this at 100 or less for maximum accuracy rightStepper.setAcceleration(100); //Set Max Acceleration of Right Stepper initialHomingRightStepper=1; //Set Right Stepper to Move right once it starts rotating while(!digitalRead(rightStepperHomingSwitch)) { //Set Right Stepper to do code in following while loop until the left limit switch is deactivated rightStepper.moveTo(-initialHomingRightStepper); //Stepper will move right rightStepper.run(); //Starts Moving the Right Stepper initialHomingRightStepper++; //Keeps Right Stepper Rotating delay(5); //Slows Down While Loop so Arduino does not have to process so much data } rightStepper.setCurrentPosition(0); //The Right Stepper is now set to an initial position of 0 and is ready for the rest of the program to run Serial.println("Right Stepper - Homing Complete"); //Print "Right Stepper - Homing Complete" //-------------------------------------------------------------------------------- END - Homing Steppers - DO NOT EDIT --------------------------------------------------------------------\\ } }