PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

56 lines
1.4KB

  1. /* Teensy 4 H/S Encoder Library - TwoKnobs Example
  2. * http://www.pjrc.com/teensy/td_libs_Encoder.html
  3. *
  4. * This example code is in the public domain.
  5. */
  6. #include "QuadEncoder.h"
  7. // Change these pin numbers to the pins connected to your encoder.
  8. // Allowable encoder pins:
  9. // 0, 1, 2, 3, 4, 5, 7, 30, 31 and 33
  10. // Encoder on channel 1 of 4 available
  11. // Phase A (pin0), PhaseB(pin1),
  12. QuadEncoder knobLeft(1, 0, 1);
  13. // Encoder on channel 2 of 4 available
  14. //Phase A (pin2), PhaseB(pin3), Pullups Req(0)
  15. QuadEncoder knobRight(2, 2, 3);
  16. // avoid using pins with LEDs attached
  17. void setup() {
  18. Serial.begin(9600);
  19. Serial.println("TwoKnobs Encoder Test:");
  20. /* Initialize Encoder/knobLeft. */
  21. knobLeft.setInitConfig();
  22. knobLeft.init();
  23. /* Initialize Encoder/knobRight. */
  24. knobRight.setInitConfig();
  25. knobRight.init();
  26. }
  27. long positionLeft = -999;
  28. long positionRight = -999;
  29. void loop() {
  30. long newLeft, newRight;
  31. newLeft = knobLeft.read();
  32. newRight = knobRight.read();
  33. if (newLeft != positionLeft || newRight != positionRight) {
  34. Serial.print("Left = ");
  35. Serial.print(newLeft);
  36. Serial.print(", Right = ");
  37. Serial.print(newRight);
  38. Serial.println();
  39. positionLeft = newLeft;
  40. positionRight = newRight;
  41. }
  42. // if a character is sent from the serial monitor,
  43. // reset both back to zero.
  44. if (Serial.available()) {
  45. Serial.read();
  46. Serial.println("Reset both knobs to zero");
  47. knobLeft.write(0);
  48. knobRight.write(0);
  49. }
  50. }