Advertisement
j0h

2RotaryEncoders

j0h
Nov 22nd, 2021
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. /* Rotary Encoder example code, modified to
  2.  *  read 2 rotary encoders (A,B) and thier switches.
  3.  *  https://lastminuteengineers.com/rotary-encoder-arduino-tutorial/
  4. */
  5. // Rotary Encoder Inputs
  6. #define CLKA 2
  7. #define DTA 3
  8. #define SWA 4
  9.  
  10. #define CLKB 5
  11. #define DTB 6
  12. #define SWB 7
  13.  
  14. int Acounter = 0;
  15. int Bcounter = 0;
  16.  
  17. int AcurrentStateCLK;
  18. int BcurrentStateCLK;
  19.  
  20. int AlastStateCLK;
  21. int BlastStateCLK;
  22.  
  23. String AcurrentDir ="";  //just easier than cleverly combining this string for seprate IO
  24. String BcurrentDir ="";
  25.  
  26. unsigned long AlastButtonPress = 0;
  27. unsigned long BlastButtonPress = 0;
  28.  
  29. /*Function Defs*/
  30. void ReadA(int AcurrentStateCLK);
  31. void ReadB(int BcurrentStateCLK);
  32.  
  33. void setup() {
  34.  
  35.   // Set encoder pins as inputs
  36.   pinMode(CLKA,INPUT);
  37.   pinMode(DTA,INPUT);
  38.   pinMode(SWA, INPUT_PULLUP);
  39.   pinMode(CLKB,INPUT);
  40.   pinMode(DTB,INPUT);
  41.   pinMode(SWB, INPUT_PULLUP);
  42.  
  43.   // Setup Serial Monitor
  44.   Serial.begin(9600);
  45.  
  46.   // Read the initial state of CLK
  47.   AlastStateCLK = digitalRead(CLKA);
  48.   BlastStateCLK = digitalRead(CLKB);
  49.  
  50. }
  51.  
  52. void loop() {
  53.  
  54.   // Read the current state of CLK
  55.   AcurrentStateCLK = digitalRead(CLKA);
  56.   BcurrentStateCLK = digitalRead(CLKB);
  57.   // If last and current state of CLK are different, then pulse occurred
  58.   // React to only 1 state change to avoid double count
  59.   if (AcurrentStateCLK != AlastStateCLK  && AcurrentStateCLK == 1){
  60.  
  61.     // If the DT state is different than the CLK state then
  62.     // the encoder is rotating CCW so decrement
  63.     if (digitalRead(DTA) != AcurrentStateCLK) {
  64.       Acounter --;
  65.       AcurrentDir ="CCW";
  66.     } else {
  67.       // Encoder is rotating CW so increment
  68.       Acounter ++;
  69.       AcurrentDir ="CW";
  70.     }
  71.  
  72.     Serial.print("A Direction: ");
  73.     Serial.print(AcurrentDir);
  74.     Serial.print(" | Counter: ");
  75.     Serial.println(Acounter);
  76.   }
  77.  
  78.   if (BcurrentStateCLK != BlastStateCLK  && BcurrentStateCLK == 1){
  79.  
  80.     // If the DT state is different than the CLK state then
  81.     // the encoder is rotating CCW so decrement
  82.     if (digitalRead(DTB) != BcurrentStateCLK) {
  83.       Bcounter --;
  84.       BcurrentDir ="CCW";
  85.     } else {
  86.       // Encoder is rotating CW so increment
  87.       Bcounter ++;
  88.       BcurrentDir ="CW";
  89.     }
  90.  
  91.     Serial.print("B Direction: ");
  92.     Serial.print(BcurrentDir);
  93.     Serial.print(" | Counter: ");
  94.     Serial.println(Bcounter);
  95.   }
  96.  
  97.   // Remember last CLK state
  98.   AlastStateCLK = AcurrentStateCLK;
  99.   BlastStateCLK = BcurrentStateCLK;
  100.  
  101.   // Read the button state
  102.   int AbtnState = digitalRead(SWA);
  103.   int BbtnState = digitalRead(SWB);
  104.  
  105.   //If we detect LOW signal, button is pressed
  106.   if (AbtnState == LOW) {
  107.     //if 50ms have passed since last LOW pulse, it means that the
  108.     //button has been pressed, released and pressed again
  109.     if (millis() - AlastButtonPress > 500) {
  110.       Serial.println("Button A pressed!");
  111.     }
  112.   AlastButtonPress = millis();
  113.   }
  114.   if (BbtnState == LOW) {
  115.     //if 50ms have passed since last LOW pulse, it means that the
  116.     //button has been pressed, released and pressed again
  117.     if (millis() - BlastButtonPress > 50) {
  118.       Serial.println("Button B pressed!");
  119.     }
  120.  
  121.  // Remember last button press event
  122.     BlastButtonPress = millis();
  123.   }
  124.  
  125.  // Put in a slight delay to help debounce the reading
  126.   delay(1);
  127.  
  128. } //end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement