Guest User

Untitled

a guest
Oct 30th, 2014
252
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <digitalWriteFast.h> // library for high performance reads and writes by jrraines
  2.  
  3. #define c_LeftEncoderInterrupt 0
  4. #define c_LeftEncoderPinA 2
  5. #define c_LeftEncoderPinB 3
  6.  
  7. volatile bool _LeftEncoderBSet;
  8. volatile long _LeftEncoderTicks = 0;
  9.  
  10. void setup()
  11. {
  12. Serial.begin(115200);
  13. pinMode(c_LeftEncoderPinA, INPUT);
  14. digitalWrite(c_LeftEncoderPinA, LOW);
  15. pinMode(c_LeftEncoderPinB, INPUT);
  16. digitalWrite(c_LeftEncoderPinB, LOW);
  17. attachInterrupt(c_LeftEncoderInterrupt, HandleLeftMotorInterruptA, RISING);
  18. }
  19.  
  20. // Interrupt service routines for the left motor's quadrature encoder
  21. void HandleLeftMotorInterruptA()
  22. {
  23. _LeftEncoderBSet = digitalReadFast2(c_LeftEncoderPinB); // read the input pin
  24. _LeftEncoderTicks -= _LeftEncoderBSet ? -1 : +1;
  25. }
  26.  
  27. void loop()
  28. {
  29. Serial.print(_LeftEncoderTicks);
  30. Serial.print("\n");
  31. delay(20);
  32. }
RAW Paste Data