Advertisement
j0h

2RotaryEncoders_in_function blocks

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