Advertisement
pleasedontcode

Encoder Events rev_02

Jun 22nd, 2024
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Encoder Events
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-06-22 20:56:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop an Arduino project to interface with two */
  21.     /* KY-040 rotary encoders. Use SimpleEncoder and */
  22.     /* EncButton libraries to manage encoder and button */
  23.     /* inputs. Configure pins D2-D7 and display encoder */
  24.     /* positions and button states on Serial Monitor. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EncButton.h>  // https://github.com/GyverLibs/EncButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void myTurn1();
  35. void myRight1();
  36. void myLeft1();
  37. void myClick1();
  38. void myHolded1();
  39. void myPress1();
  40. void myRelease1();
  41. void myTurn2();
  42. void myRight2();
  43. void myLeft2();
  44. void myClick2();
  45. void myHolded2();
  46. void myPress2();
  47. void myRelease2();
  48.  
  49. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  50. const uint8_t _1enc_KY040_CLK_PIN_D2 = 2;
  51. const uint8_t _1enc_KY040_DT_PIN_D3 = 3;
  52. const uint8_t _1enc_KY040_SW_PIN_D4 = 4;
  53. const uint8_t _2enc_KY040_CLK_PIN_D5 = 5;
  54. const uint8_t _2enc_KY040_DT_PIN_D6 = 6;
  55. const uint8_t _2enc_KY040_SW_PIN_D7 = 7;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. EncButton<EB_CALLBACK, _1enc_KY040_CLK_PIN_D2, _1enc_KY040_DT_PIN_D3, _1enc_KY040_SW_PIN_D4> enc1;
  59. EncButton<EB_CALLBACK, _2enc_KY040_CLK_PIN_D5, _2enc_KY040_DT_PIN_D6, _2enc_KY040_SW_PIN_D7> enc2;
  60.  
  61. void setup(void)
  62. {
  63.   // Initialize serial communication
  64.   Serial.begin(9600);
  65.  
  66.   // Configure pins for the first encoder
  67.   pinMode(_1enc_KY040_CLK_PIN_D2, INPUT);
  68.   pinMode(_1enc_KY040_DT_PIN_D3, INPUT);
  69.   pinMode(_1enc_KY040_SW_PIN_D4, INPUT_PULLUP);
  70.  
  71.   // Configure pins for the second encoder
  72.   pinMode(_2enc_KY040_CLK_PIN_D5, INPUT);
  73.   pinMode(_2enc_KY040_DT_PIN_D6, INPUT);
  74.   pinMode(_2enc_KY040_SW_PIN_D7, INPUT_PULLUP);
  75.  
  76.   // Attach handlers for various events for the first encoder
  77.   enc1.attach(TURN_HANDLER, myTurn1);
  78.   enc1.attach(RIGHT_HANDLER, myRight1);
  79.   enc1.attach(LEFT_HANDLER, myLeft1);
  80.   enc1.attach(CLICK_HANDLER, myClick1);
  81.   enc1.attach(HOLDED_HANDLER, myHolded1);
  82.   enc1.attach(PRESS_HANDLER, myPress1);
  83.   enc1.attach(RELEASE_HANDLER, myRelease1);
  84.  
  85.   // Attach handlers for various events for the second encoder
  86.   enc2.attach(TURN_HANDLER, myTurn2);
  87.   enc2.attach(RIGHT_HANDLER, myRight2);
  88.   enc2.attach(LEFT_HANDLER, myLeft2);
  89.   enc2.attach(CLICK_HANDLER, myClick2);
  90.   enc2.attach(HOLDED_HANDLER, myHolded2);
  91.   enc2.attach(PRESS_HANDLER, myPress2);
  92.   enc2.attach(RELEASE_HANDLER, myRelease2);
  93. }
  94.  
  95. // Event handlers for the first encoder
  96. void myTurn1() {
  97.   Serial.print("Encoder 1 TURN_HANDLER: ");
  98.   Serial.println(enc1.counter);  // Print the counter value
  99. }
  100.  
  101. void myRight1() {
  102.   Serial.println("Encoder 1 RIGHT_HANDLER");
  103. }
  104.  
  105. void myLeft1() {
  106.   Serial.println("Encoder 1 LEFT_HANDLER");
  107. }
  108.  
  109. void myClick1() {
  110.   Serial.println("Encoder 1 CLICK_HANDLER");
  111. }
  112.  
  113. void myHolded1() {
  114.   Serial.println("Encoder 1 HOLDED_HANDLER");
  115. }
  116.  
  117. void myPress1() {
  118.   Serial.println("Encoder 1 PRESS_HANDLER");
  119. }
  120.  
  121. void myRelease1() {
  122.   Serial.println("Encoder 1 RELEASE_HANDLER");
  123. }
  124.  
  125. // Event handlers for the second encoder
  126. void myTurn2() {
  127.   Serial.print("Encoder 2 TURN_HANDLER: ");
  128.   Serial.println(enc2.counter);  // Print the counter value
  129. }
  130.  
  131. void myRight2() {
  132.   Serial.println("Encoder 2 RIGHT_HANDLER");
  133. }
  134.  
  135. void myLeft2() {
  136.   Serial.println("Encoder 2 LEFT_HANDLER");
  137. }
  138.  
  139. void myClick2() {
  140.   Serial.println("Encoder 2 CLICK_HANDLER");
  141. }
  142.  
  143. void myHolded2() {
  144.   Serial.println("Encoder 2 HOLDED_HANDLER");
  145. }
  146.  
  147. void myPress2() {
  148.   Serial.println("Encoder 2 PRESS_HANDLER");
  149. }
  150.  
  151. void myRelease2() {
  152.   Serial.println("Encoder 2 RELEASE_HANDLER");
  153. }
  154.  
  155. void loop(void)
  156. {
  157.   // Process the encoder and button actions for both encoders
  158.   enc1.tick();
  159.   enc2.tick();
  160. }
  161.  
  162. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement