Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AccelStepper.h>
- #define ENA1 5
- #define DIR1 6
- #define CLK1 7
- #define ENA2 8
- #define DIR2 9
- #define CLK2 10
- AccelStepper stepper1(1, DIR1, CLK1);// direction Digital 9 (CCW), pulses (or step) Digital 8 (CLK)
- AccelStepper stepper2(1, DIR2, CLK2);// direction Digital 7 (CCW), pulses (or step) Digital 6 (CLK)
- bool pingpong_CW = false;
- bool pingpong_CCW = false;
- bool pingpong_CW2 = false;
- bool pingpong_CCW2 = false;
- void setup()
- {
- pinMode(ENA1, OUTPUT);
- pinMode(ENA2, OUTPUT);
- digitalWrite(ENA1, HIGH);
- digitalWrite(ENA2, HIGH);
- //Accelstepper parameters
- stepper1.setMaxSpeed(6000); //SPEED = Steps / second
- stepper2.setMaxSpeed(6000); //SPEED = Steps / second
- stepper1.setAcceleration(5000); //ACCELERATION = Steps /(second)^2
- stepper2.setAcceleration(5000); //ACCELERATION = Steps /(second)^2
- stepper1.enableOutputs(); //enable pins
- stepper2.enableOutputs(); //enable pins
- }
- void loop()
- {
- PingPong();
- stepper1.run();
- stepper2.run();
- }
- void PingPong()
- {
- if(pingpong_CW == false) //CW rotation is not yet done
- {
- stepper1.moveTo(3000); //set a target position, it should be an absolute. relative (move()) leads to "infinite loop"
- if(stepper1.distanceToGo() == 0) //When the above number of steps are completed, we manipulate the variables
- {
- pingpong_CW = true; //CW rotation is now done
- pingpong_CCW = false; //CCW rotation is not yet done - this allows the code to enter the next ifs
- }
- }
- if(pingpong_CW == true && pingpong_CCW == false) //CW is completed and CCW is not yet done
- {
- stepper1.moveTo(0); //Absolute position
- if(stepper1.distanceToGo() == 0) //When the number of steps are completed
- {
- pingpong_CCW = true; //CCW is now done
- pingpong_CW = false; //CW is not yet done. This allows the code to enter the first if again!
- }
- }
- if(pingpong_CCW2 == false) //CW rotation is not yet done
- {
- stepper2.moveTo(2000); //set a target position, it should be an absolute. relative (move()) leads to "infinite loop"
- if(stepper2.distanceToGo() == 0) //When the above number of steps are completed, we manipulate the variables
- {
- pingpong_CCW2 = true; //CW rotation is now done
- pingpong_CW2 = false; //CCW rotation is not yet done - this allows the code to enter the next ifs
- }
- }
- if(pingpong_CCW2 == true && pingpong_CW2 == false) //CW is completed and CCW is not yet done
- {
- stepper2.moveTo(0); //Absolute position
- if(stepper2.distanceToGo() == 0) //When the number of steps are completed
- {
- pingpong_CW2 = true; //CCW is now done
- pingpong_CCW2 = false; //CW is not yet done. This allows the code to enter the first if again!
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment