Advertisement
sunu

testing syntaz

Aug 3rd, 2021
1,338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://domoticx.com/arduino-rotary-encoder-keyes-ky-040/
  2.  
  3. int clock = 2;             // Define encoder pin A
  4. int data = 3;              // Define encoder pin B
  5. int count = 0;             // pre-init the count to zero
  6. int c = LOW;               // pre-init the state of pin A low
  7. int cLast = LOW;           // and make its last val the same - ie no change
  8. int d = LOW;               // and make the data val low as well
  9.  
  10. void setup() {
  11.   pinMode (clock,INPUT);  // setup the pins as inputs
  12.   pinMode (data,INPUT);
  13.   Serial.begin (9600);    // and give some serial debugging
  14. }
  15.  
  16. void loop() {
  17.   c = digitalRead(clock); // read pin A as clock
  18.   d = digitalRead(data);  // read pin B as data
  19.  
  20.   if (c != cLast) {       // clock pin has changed value... now we can do stuff
  21.     d = c^d;              // work out direction using an XOR
  22.     if ( d ) {
  23.       count--;            // non-zero is Anti-clockwise
  24.     }else{
  25.       count++;            // zero is therefore anti-clockwise
  26.     }
  27.     Serial.print ("Jog:: count:");
  28.     Serial.println(count);
  29.     cLast = c;            // store current clock state for next pass
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement