Advertisement
AlexShu

Rotary Encoder

Nov 4th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. // Pins
  2. int encoderPin1 = 2;
  3. int encoderPin2 = 3;
  4. int encoderSwitchPin = 4; //push button switch
  5.  
  6. // Encoder Magic
  7. volatile int lastEncoded = 0;
  8. volatile long encoderValue = 0;
  9.  
  10. long lastencoderValue = 0;
  11. long last =0;
  12. int val=0;
  13. int oldval=0;
  14.  
  15. int lastMSB = 0;
  16. int lastLSB = 0;
  17.  
  18. // Debounce Stuff for the push button
  19. long btn_debounce_start = 0;
  20. int btn_debounce_duration = 500;
  21.  
  22.  
  23.  
  24. void setup() {
  25.   pinMode(encoderPin1, INPUT);
  26.   pinMode(encoderPin2, INPUT);
  27.  
  28.   pinMode(encoderSwitchPin, INPUT);
  29.  
  30.  
  31.   digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  32.   digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
  33.  
  34.   digitalWrite(encoderSwitchPin, HIGH); //turn pullup resistor on
  35.  
  36.   Serial.begin(9600);
  37.  
  38. }
  39.  
  40.  
  41. void loop(void) {
  42.  
  43.     if(encoderBtnOK()){
  44.     // this is when the button is pressed
  45.     }
  46.     if(encoderBtnUp()){
  47.         // this when you turn the encoder to one side
  48.     }
  49.     if(encoderBtnDown()){
  50.         // this when you turn the encoder to another side
  51.     }
  52.  
  53.    
  54.     // Stuff for the other stuff to work :)
  55.     updateEncoder();
  56.     encoderWatchdog();
  57. }
  58.  
  59. void updateEncoder(){
  60.   int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  61.   int LSB = digitalRead(encoderPin2); //LSB = least significant bit
  62.  
  63.   int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  64.   int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  65.  
  66.   if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
  67.   if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
  68.  
  69.   lastEncoded = encoded; //store this value for next time
  70. }
  71.  
  72. void encoderWatchdog(){
  73.      //Serial.println(encoderValue);
  74.     if(last != encoderValue){
  75.         if(encoderValue == last+4){
  76.             encoderUp = true;
  77.             last=encoderValue;
  78.         }
  79.         if(encoderValue == last-4){
  80.             encoderDown = true;
  81.             last=encoderValue;
  82.         }
  83.     }
  84. }
  85.  
  86. boolean encoderBtnOK(){
  87.   if(digitalRead(encoderSwitchPin)){
  88.     if(millis() - btn_debounce_start > btn_debounce_duration){
  89.     btn_debounce_start = millis();  
  90.     return true;
  91.   }
  92.   }
  93.   return false;
  94. }
  95.  
  96. boolean encoderBtnUp(){
  97.   if(encoderUp == true){
  98.     encoderUp = false;
  99.     return true;
  100.   }
  101.   return false;
  102. }
  103.  
  104. boolean encoderBtnDown(){
  105.   if(encoderDown == true){
  106.     encoderDown = false;
  107.     return true;
  108.   }
  109.   return false;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement