elektronek

Víťa Kolář - krokáče

Nov 5th, 2020 (edited)
698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <AccelStepper.h>
  2. #define ENA1    5
  3. #define DIR1    6
  4. #define CLK1    7
  5. #define ENA2    8
  6. #define DIR2    9
  7. #define CLK2    10
  8.  
  9. AccelStepper stepper1(1, DIR1, CLK1);// direction Digital 9 (CCW), pulses (or step) Digital 8 (CLK)
  10. AccelStepper stepper2(1, DIR2, CLK2);// direction Digital 7 (CCW), pulses (or step) Digital 6 (CLK)
  11. bool pingpong_CW = false;
  12. bool pingpong_CCW = false;
  13. bool pingpong_CW2 = false;
  14. bool pingpong_CCW2 = false;
  15.  
  16. void setup()
  17. {
  18.     pinMode(ENA1, OUTPUT);
  19.     pinMode(ENA2, OUTPUT);
  20.     digitalWrite(ENA1, HIGH);
  21.     digitalWrite(ENA2, HIGH);    
  22.  
  23.     //Accelstepper parameters
  24.     stepper1.setMaxSpeed(6000); //SPEED = Steps / second
  25.     stepper2.setMaxSpeed(6000); //SPEED = Steps / second
  26.     stepper1.setAcceleration(5000); //ACCELERATION = Steps /(second)^2
  27.     stepper2.setAcceleration(5000); //ACCELERATION = Steps /(second)^2
  28.     stepper1.enableOutputs(); //enable pins
  29.     stepper2.enableOutputs(); //enable pins
  30. }
  31.  
  32. void loop()
  33. {
  34.     PingPong();
  35.     stepper1.run();
  36.     stepper2.run();
  37. }
  38.  
  39. void PingPong()
  40. {
  41.     if(pingpong_CW == false) //CW rotation is not yet done
  42.     {  
  43.         stepper1.moveTo(3000); //set a target position, it should be an absolute. relative (move()) leads to "infinite loop"
  44.         if(stepper1.distanceToGo() == 0) //When the above number of steps are completed, we manipulate the variables
  45.         {
  46.             pingpong_CW = true; //CW rotation is now done
  47.             pingpong_CCW = false; //CCW rotation is not yet done - this allows the code to enter the next ifs
  48.         }      
  49.     }
  50.  
  51.     if(pingpong_CW == true && pingpong_CCW == false) //CW is completed and CCW is not yet done
  52.     {
  53.         stepper1.moveTo(0); //Absolute position
  54.         if(stepper1.distanceToGo() == 0) //When the number of steps are completed
  55.             {
  56.             pingpong_CCW = true; //CCW is now done
  57.             pingpong_CW = false; //CW is not yet done. This allows the code to enter the first if again!
  58.         }    
  59.     }
  60.  
  61.     if(pingpong_CCW2 == false) //CW rotation is not yet done
  62.     {  
  63.         stepper2.moveTo(2000); //set a target position, it should be an absolute. relative (move()) leads to "infinite loop"
  64.         if(stepper2.distanceToGo() == 0) //When the above number of steps are completed, we manipulate the variables
  65.         {
  66.             pingpong_CCW2 = true; //CW rotation is now done
  67.             pingpong_CW2 = false; //CCW rotation is not yet done - this allows the code to enter the next ifs
  68.         }      
  69.     }
  70.  
  71.     if(pingpong_CCW2 == true && pingpong_CW2 == false) //CW is completed and CCW is not yet done
  72.     {
  73.             stepper2.moveTo(0); //Absolute position
  74.         if(stepper2.distanceToGo() == 0) //When the number of steps are completed
  75.         {
  76.             pingpong_CW2 = true; //CCW is now done
  77.             pingpong_CCW2 = false; //CW is not yet done. This allows the code to enter the first if again!
  78.         }    
  79.     }
  80. }
RAW Paste Data