Advertisement
pleasedontcode

Encoder Manager rev_01

Jun 22nd, 2024
547
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 Manager
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-06-22 18:19:22
  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. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EncButton.h>  // https://github.com/GyverLibs/EncButton
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void myTurn1();
  34. void myRight1();
  35. void myLeft1();
  36. void myClick1();
  37. void myHolded1();
  38. void myPress1();
  39. void myRelease1();
  40. void myTurn2();
  41. void myRight2();
  42. void myLeft2();
  43. void myClick2();
  44. void myHolded2();
  45. void myPress2();
  46. void myRelease2();
  47.  
  48. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  49. const uint8_t _1enc_KY040_CLK_PIN_D2 = 2;
  50. const uint8_t _1enc_KY040_DT_PIN_D3 = 3;
  51. const uint8_t _1enc_KY040_SW_PIN_D4 = 4;
  52. const uint8_t _2enc_KY040_CLK_PIN_D5 = 5;
  53. const uint8_t _2enc_KY040_DT_PIN_D6 = 6;
  54. const uint8_t _2enc_KY040_SW_PIN_D7 = 7;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. EncButton<EB_CALLBACK, _1enc_KY040_CLK_PIN_D2, _1enc_KY040_DT_PIN_D3, _1enc_KY040_SW_PIN_D4> enc1;
  58. EncButton<EB_CALLBACK, _2enc_KY040_CLK_PIN_D5, _2enc_KY040_DT_PIN_D6, _2enc_KY040_SW_PIN_D7> enc2;
  59.  
  60. void setup(void)
  61. {
  62.   // Initialize serial communication
  63.   Serial.begin(9600);
  64.  
  65.   // Configure pins for the first encoder
  66.   pinMode(_1enc_KY040_CLK_PIN_D2, INPUT);
  67.   pinMode(_1enc_KY040_DT_PIN_D3, INPUT);
  68.   pinMode(_1enc_KY040_SW_PIN_D4, INPUT_PULLUP);
  69.  
  70.   // Configure pins for the second encoder
  71.   pinMode(_2enc_KY040_CLK_PIN_D5, INPUT);
  72.   pinMode(_2enc_KY040_DT_PIN_D6, INPUT);
  73.   pinMode(_2enc_KY040_SW_PIN_D7, INPUT_PULLUP);
  74.  
  75.   // Attach handlers for various events for the first encoder
  76.   enc1.attach(TURN_HANDLER, myTurn1);
  77.   enc1.attach(RIGHT_HANDLER, myRight1);
  78.   enc1.attach(LEFT_HANDLER, myLeft1);
  79.   enc1.attach(CLICK_HANDLER, myClick1);
  80.   enc1.attach(HOLDED_HANDLER, myHolded1);
  81.   enc1.attach(PRESS_HANDLER, myPress1);
  82.   enc1.attach(RELEASE_HANDLER, myRelease1);
  83.  
  84.   // Attach handlers for various events for the second encoder
  85.   enc2.attach(TURN_HANDLER, myTurn2);
  86.   enc2.attach(RIGHT_HANDLER, myRight2);
  87.   enc2.attach(LEFT_HANDLER, myLeft2);
  88.   enc2.attach(CLICK_HANDLER, myClick2);
  89.   enc2.attach(HOLDED_HANDLER, myHolded2);
  90.   enc2.attach(PRESS_HANDLER, myPress2);
  91.   enc2.attach(RELEASE_HANDLER, myRelease2);
  92. }
  93.  
  94. // Event handlers for the first encoder
  95. void myTurn1() {
  96.   Serial.print("Encoder 1 TURN_HANDLER: ");
  97.   Serial.println(enc1.counter);  // Print the counter value
  98. }
  99.  
  100. void myRight1() {
  101.   Serial.println("Encoder 1 RIGHT_HANDLER");
  102. }
  103.  
  104. void myLeft1() {
  105.   Serial.println("Encoder 1 LEFT_HANDLER");
  106. }
  107.  
  108. void myClick1() {
  109.   Serial.println("Encoder 1 CLICK_HANDLER");
  110. }
  111.  
  112. void myHolded1() {
  113.   Serial.println("Encoder 1 HOLDED_HANDLER");
  114. }
  115.  
  116. void myPress1() {
  117.   Serial.println("Encoder 1 PRESS_HANDLER");
  118. }
  119.  
  120. void myRelease1() {
  121.   Serial.println("Encoder 1 RELEASE_HANDLER");
  122. }
  123.  
  124. // Event handlers for the second encoder
  125. void myTurn2() {
  126.   Serial.print("Encoder 2 TURN_HANDLER: ");
  127.   Serial.println(enc2.counter);  // Print the counter value
  128. }
  129.  
  130. void myRight2() {
  131.   Serial.println("Encoder 2 RIGHT_HANDLER");
  132. }
  133.  
  134. void myLeft2() {
  135.   Serial.println("Encoder 2 LEFT_HANDLER");
  136. }
  137.  
  138. void myClick2() {
  139.   Serial.println("Encoder 2 CLICK_HANDLER");
  140. }
  141.  
  142. void myHolded2() {
  143.   Serial.println("Encoder 2 HOLDED_HANDLER");
  144. }
  145.  
  146. void myPress2() {
  147.   Serial.println("Encoder 2 PRESS_HANDLER");
  148. }
  149.  
  150. void myRelease2() {
  151.   Serial.println("Encoder 2 RELEASE_HANDLER");
  152. }
  153.  
  154. void loop(void)
  155. {
  156.   // Process the encoder and button actions for both encoders
  157.   enc1.tick();
  158.   enc2.tick();
  159. }
  160.  
  161. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement