Advertisement
DuboisP

Stepper with Encoder

Jan 31st, 2025
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | Source Code | 0 0
  1. //www.elegoo.com
  2. //2016.12.12
  3.  
  4. #include "Stepper.h"
  5. #define STEPS  32   // Number of steps for one revolution of Internal shaft
  6.                     // 2048 steps for one revolution of External shaft
  7.  
  8. volatile boolean TurnDetected;  // need volatile for Interrupts
  9. volatile boolean rotationdirection;  // CW or CCW rotation
  10.  
  11. const int PinCLK=2;   // Generating interrupts using CLK signal
  12. const int PinDT=3;    // Reading DT signal
  13. const int PinSW=4;    // Reading Push Button switch
  14.  
  15. int RotaryPosition=0;    // To store Stepper Motor Position
  16.  
  17. int PrevPosition;     // Previous Rotary position Value to check accuracy
  18. int StepsToTake;      // How much to move Stepper
  19.  
  20. // Setup of proper sequencing for Motor Driver Pins
  21. // In1, In2, In3, In4 in the sequence 1-3-2-4
  22. Stepper small_stepper(STEPS, 8, 10, 9, 11);
  23.  
  24. // Interrupt routine runs if CLK goes from HIGH to LOW
  25. void isr ()  {
  26.   delay(4);  // delay for Debouncing
  27.   if (digitalRead(PinCLK))
  28.     rotationdirection= digitalRead(PinDT);
  29.   else
  30.     rotationdirection= !digitalRead(PinDT);
  31.   TurnDetected = true;
  32. }
  33.  
  34. void setup ()  {
  35.  
  36. pinMode(PinCLK,INPUT);
  37. pinMode(PinDT,INPUT);  
  38. pinMode(PinSW,INPUT);
  39. digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
  40. attachInterrupt (0,isr,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
  41. }
  42.  
  43. void loop ()  {
  44.   small_stepper.setSpeed(700); //Max seems to be 700
  45.   if (!(digitalRead(PinSW))) {   // check if button is pressed
  46.     if (RotaryPosition == 0) {  // check if button was already pressed
  47.     } else {
  48.         small_stepper.step(-(RotaryPosition*50));
  49.         RotaryPosition=0; // Reset position to ZERO
  50.       }
  51.     }
  52.  
  53.   // Runs if rotation was detected
  54.   if (TurnDetected)  {
  55.     PrevPosition = RotaryPosition; // Save previous position in variable
  56.     if (rotationdirection) {
  57.       RotaryPosition=RotaryPosition-1;} // decrase Position by 1
  58.     else {
  59.       RotaryPosition=RotaryPosition+1;} // increase Position by 1
  60.  
  61.     TurnDetected = false;  // do NOT repeat IF loop until new rotation detected
  62.  
  63.     // Which direction to move Stepper motor
  64.     if ((PrevPosition + 1) == RotaryPosition) { // Move motor CW
  65.       StepsToTake=50;
  66.       small_stepper.step(StepsToTake);
  67.     }
  68.  
  69.     if ((RotaryPosition + 1) == PrevPosition) { // Move motor CCW
  70.       StepsToTake=-50;
  71.       small_stepper.step(StepsToTake);
  72.     }
  73.   }
  74.      digitalWrite(8, LOW);
  75.      digitalWrite(9, LOW);
  76.      digitalWrite(10, LOW);
  77.      digitalWrite(11, LOW);    
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement