CuriousScientist

Arduino code for rotary encoder and Accelstepper

Apr 4th, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
  3. //This code belongs to the following tutorial video: https://youtu.be/YGnxGJmxHlo
  4.  
  5. //AccelStepper
  6. #include <AccelStepper.h>
  7. AccelStepper stepper(1, 9, 8);// pulses Digital 9 (CLK); Direction Digital 8 (CCW)
  8.  
  9. //16x2 LCD
  10. #include <LiquidCrystal_I2C.h> //SDA = A4, SCL = A5
  11. LiquidCrystal_I2C lcd(0x27, 16, 2);
  12.  
  13.  
  14. //Defining pins
  15. const int RotaryCLK = 2; //CLK pin on the rotary encoder
  16. const int RotaryDT = 4; //DT pin on the rotary encoder
  17. const int RotarySW = 3; //SW pin on the rotary encoder (Button function)
  18.  
  19. //Defining variables
  20. int ButtonCounter = 0;
  21. int RotateCounter = 0;
  22.  
  23. //Statuses
  24. int CLKNow;
  25. int CLKPrevious;
  26.  
  27. int DTNow;
  28. int DTPrevious;
  29.  
  30. // Time
  31. float TimeNow1;
  32. float TimeNow2;
  33.  
  34.  
  35.  
  36. void setup()
  37. {
  38.  
  39.   Serial.begin(9600); //can be skipped, if you won't use the serial terminal. I kept it for debugging
  40.  
  41.     //------------------------------------------------------
  42.     lcd.init();                      // initialize the lcd
  43.     lcd.init();
  44.     lcd.backlight();
  45.     //------------------------------------------------------
  46.   lcd.setCursor(0,0); //Defining positon to write from first row,first column .
  47.   lcd.print("Rotary encoder");
  48.   lcd.setCursor(0,1);
  49.   lcd.print("Stepper stepping"); //You can write 16 Characters per line .
  50.   delay(5000); //wait 1 sec
  51.   //------------------------------------------------------
  52.   //internal pullups
  53.    pinMode(2, INPUT_PULLUP);
  54.    pinMode(3, INPUT_PULLUP);
  55.    pinMode(4, INPUT_PULLUP);    
  56.  
  57.   //Store states
  58.   CLKPrevious = digitalRead(RotaryCLK);
  59.   DTPrevious = digitalRead(RotaryDT);
  60.    
  61.   attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE); //attachinterrupt for the rotation
  62.   attachInterrupt(digitalPinToInterrupt(RotarySW), buttonPressed, FALLING); //attachinterrupt for the click (button)
  63.  
  64.   stepper.setMaxSpeed(1000); //SPEED = Steps / second
  65.   stepper.setAcceleration(5000); //ACCELERATION = Steps /(second)^2
  66.  
  67.   TimeNow1 = millis(); //Start time
  68.  
  69. }
  70.  
  71.  
  72. void loop()
  73. {
  74.   printLCD();
  75.   RunTheMotor();
  76. }
  77.  
  78. void buttonPressed()
  79. {  
  80.   TimeNow2 = millis();
  81.   if(TimeNow2 - TimeNow1 > 1000) // if two clicks are closer to each other than 1s, they are neglected. sort of a debouncing.
  82.   {
  83.   ButtonCounter++; //increase the counter
  84.   }
  85.   TimeNow1 = millis();
  86.     //You can add something here, like resetting the RotateCounter (e.g. redefine 0 position)
  87. }
  88.  
  89. void rotate()
  90. {
  91.   CLKNow = digitalRead(RotaryCLK); //Read the state of the CLK pin
  92.  
  93.   // If last and current state of CLK are different, then a pulse occurred  
  94.   if (CLKNow != CLKPrevious  && CLKNow == 1){
  95.  
  96.     // If the DT state is different than the CLK state then
  97.     // the encoder is rotating CCW so increase
  98.     if (digitalRead(RotaryDT) != CLKNow) {
  99.       RotateCounter++;
  100.      
  101.     } else {
  102.       // Encoder is rotating CW so decrease
  103.       RotateCounter--;      
  104.     }    
  105.   }
  106.   CLKPrevious = CLKNow;  // Store last CLK state
  107. }
  108.  
  109.  
  110. void printLCD()
  111. {
  112.    
  113.     //lcd.clear();
  114.     lcd.setCursor(0,0); // Defining positon to write from first row, first column .
  115.     lcd.print("Clicks: ");
  116.     lcd.setCursor(8,0);
  117.     lcd.print("        ");
  118.     lcd.setCursor(8,0);
  119.     lcd.print(ButtonCounter);    //Print the number of button clicks
  120.    
  121.     lcd.setCursor(0,1); // Defining positon to write from second row, first column .
  122.     lcd.print("Position: ");
  123.     lcd.setCursor(10,1);
  124.     lcd.print("      ");
  125.     lcd.setCursor(10,1);
  126.     lcd.print(RotateCounter);    //Print the number of pulses
  127.    
  128. }
  129.  
  130. void RunTheMotor() //function for the motor
  131. {    
  132.     stepper.enableOutputs(); //enable pins
  133.     stepper.moveTo(-1*RotateCounter); //-1 is to match the rotation of the encoder with the rotation of the stepper
  134.     while(stepper.distanceToGo() != 0)
  135.     {
  136.       stepper.runToNewPosition(-1*RotateCounter);
  137.     }
  138.    
  139. }
Add Comment
Please, Sign In to add comment