Advertisement
Zaganu

encoder working 6.4.2020 daniel

Jun 4th, 2020
1,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. byte cha = 18;
  2. byte chb = 19;
  3. long encoder_pos = 0;
  4. byte last_value = B00000000;
  5. byte counter = 0;
  6. byte cumulator = B00000000;
  7. void setup() {
  8.   pinMode(cha, INPUT_PULLUP);
  9.   pinMode(chb, INPUT_PULLUP);
  10.   attachInterrupt(4, doEncoder, CHANGE);
  11.   Serial.begin(2000000);
  12. }
  13.  
  14. void loop() {
  15.   // put your main code here, to run repeatedly:
  16.   Serial.println(encoder_pos);
  17. }
  18.  
  19. void doEncoder()
  20. {
  21.   // pp B11001001
  22.   byte current_value = PIND & B00001100;  // excludem din portu D biti nefolositi => B00001000
  23.   if (last_value != current_value )  // B00001100 -> B00001000
  24.   {
  25.     cumulator <<= 2;   // B00001100 => B00110000
  26.     cumulator |= current_value >> 2; // 1. B00001000 => B00000010 , 2.B00110010
  27.     if (cumulator == 75)
  28.     {
  29.       encoder_pos --;
  30.     }
  31.     if (cumulator == 30)
  32.     {
  33.       encoder_pos ++;
  34.     }
  35.   }
  36.   last_value = current_value;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement