Guest User

Arduino - Rotary encoder serial coms

a guest
Oct 6th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. int val;
  2. int encoder0PinA = 5;
  3. int encoder0PinB = 6;
  4. int encoder0Direction = 0;
  5. int encoder0PinALast = LOW;
  6. int n = LOW;
  7.  
  8. int pingTimeout = 1000;
  9. unsigned long lastPing = 0;
  10.  
  11.  
  12. void setup() {
  13.   pinMode (encoder0PinA,INPUT);
  14.   pinMode (encoder0PinB,INPUT);
  15.   Serial.begin (115200);
  16. }
  17.  
  18. void loop() {
  19.   n = digitalRead(encoder0PinA);
  20.   if ((encoder0PinALast == LOW) && (n == HIGH)) {
  21.     if (digitalRead(encoder0PinB) == LOW) {
  22.       encoder0Direction = -1;
  23.     } else {
  24.       encoder0Direction = 1;
  25.     }
  26.     Serial.println (encoder0Direction);
  27.   }
  28.   else{
  29.     if(millis()-lastPing >= pingTimeout || millis() < 1000){
  30.       Serial.println (0);
  31.       lastPing = millis();
  32.     }
  33.   }
  34.   encoder0PinALast = n;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment