Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. // EasyDriver connections
  2. #define step_pin 9 // Pin 9 connected to Steps pin on EasyDriver
  3. #define dir_pin 8 // Pin 8 connected to Direction pin
  4. #define MS1 10 // Pin 10 connected to MS1 pin
  5. #define MS2 11 // Pin 11 connected to MS2 pin
  6. #define SLEEP 12 // Pin 12 connected to SLEEP pin
  7.  
  8. volatile boolean TurnDetected; // need volatile for Interrupts
  9. volatile boolean rotationdirection; // CW or CCW rotation
  10.  
  11. // Rotary Encoder Module connections
  12. const int PinCLK=2; // Generating interrupts using CLK signal
  13. const int PinDT=3; // Reading DT signal
  14. const int PinSW=4; // Reading Push Button switch
  15.  
  16. int StepperPosition=0; // To store Stepper Motor Position
  17. int StepsToTake=4; // Controls the speed of the Stepper per Rotary click
  18.  
  19. int direction; // Variable to set Rotation (CW-CCW) of stepper
  20.  
  21.  
  22. // Interrupt routine runs if CLK goes from HIGH to LOW
  23. void rotarydetect () {
  24. delay(4); // delay for Debouncing
  25. if (digitalRead(PinCLK))
  26. rotationdirection= digitalRead(PinDT);
  27. else
  28. rotationdirection= !digitalRead(PinDT);
  29. TurnDetected = true;
  30. }
  31.  
  32.  
  33. void setup () {
  34.  
  35. pinMode(MS1, OUTPUT);
  36. pinMode(MS2, OUTPUT);
  37. pinMode(dir_pin, OUTPUT);
  38. pinMode(step_pin, OUTPUT);
  39. pinMode(SLEEP, OUTPUT);
  40. digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
  41. delay(5); // Wait for EasyDriver wake up
  42.  
  43. /* Configure type of Steps on EasyDriver:
  44. // MS1 MS2
  45. //
  46. // LOW LOW = Full Step //
  47. // HIGH LOW = Half Step //
  48. // LOW HIGH = A quarter of Step //
  49. // HIGH HIGH = An eighth of Step //
  50. */
  51. digitalWrite(MS1, LOW); // Configures to Full Steps
  52. digitalWrite(MS2, LOW); // Configures to Full Steps
  53.  
  54. pinMode(PinCLK,INPUT); // Set Pin to Input
  55. pinMode(PinDT,INPUT);
  56. pinMode(PinSW,INPUT);
  57. digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
  58. attachInterrupt (0,rotarydetect,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
  59. }
  60.  
  61.  
  62. void loop () {
  63.  
  64. if (!(digitalRead(PinSW))) { // check if button is pressed
  65. if (StepperPosition == 0) { // check if button was already pressed
  66. } else {
  67. if (StepperPosition > 0) { // Stepper was moved CW
  68. while (StepperPosition != 0){ // Do until Motor position is back to ZERO
  69. digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
  70. for (int x = 1; x < StepsToTake; x++) {
  71. digitalWrite(step_pin, HIGH);
  72. delay(1);
  73. digitalWrite(step_pin, LOW);
  74. delay(1);
  75. }
  76. StepperPosition=StepperPosition-StepsToTake;
  77. }
  78. }
  79. else {
  80. while (StepperPosition != 0){
  81. digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
  82. for (int x = 1; x < StepsToTake; x++) {
  83. digitalWrite(step_pin, HIGH);
  84. delay(1);
  85. digitalWrite(step_pin, LOW);
  86. delay(1);
  87. }
  88. StepperPosition=StepperPosition+StepsToTake;
  89. }
  90. }
  91. StepperPosition=0; // Reset position to ZERO after moving motor back
  92. }
  93. }
  94.  
  95. // Runs if rotation was detected
  96. if (TurnDetected) {
  97. TurnDetected = false; // do NOT repeat IF loop until new rotation detected
  98.  
  99. // Which direction to move Stepper motor
  100. if (rotationdirection) { // Move motor CCW
  101. digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
  102. for (int x = 1; x < StepsToTake; x++) {
  103. digitalWrite(step_pin, HIGH);
  104. delay(1);
  105. digitalWrite(step_pin, LOW);
  106. delay(1);
  107. }
  108. StepperPosition=StepperPosition-StepsToTake;
  109. }
  110.  
  111. if (!rotationdirection) { // Move motor CW
  112. digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
  113. for (int x = 1; x < StepsToTake; x++) {
  114. digitalWrite(step_pin, HIGH);
  115. delay(1);
  116. digitalWrite(step_pin, LOW);
  117. delay(1);
  118. }
  119. StepperPosition=StepperPosition+StepsToTake;
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement