Advertisement
edovino

Untitled

Jan 25th, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <Adafruit_GFX.h>
  2. #include <Adafruit_ST7735.h>
  3. #include <ezButton.h> // the library to use for SW pin on encoder
  4.  
  5. #define CLK_PIN 34 // ESP32 pin GPIO25 connected to the rotary encoder's CLK pin
  6. #define DT_PIN 35 // ESP32 pin GPIO26 connected to the rotary encoder's DT pin
  7. #define SW_PIN 32 // ESP32 pin GPIO27 connected to the rotary encoder's SW pin
  8.  
  9. #define CLK_PIN2 33 // ESP32 2nd connected rotary encoder's CLK pin
  10. #define DT_PIN2 25 // ESP32 2nd connected rotary encoder's DT pin
  11. #define SW_PIN2 13 // ESP32 2nd connected rotary encoder's SW pin
  12.  
  13. #define DIRECTION_CW 0 // clockwise direction rotary
  14. #define DIRECTION_CCW 1 // counter-clockwise direction rotary
  15.  
  16. #define TFT_CS 5
  17. #define TFT_RST 4
  18. #define TFT_DC 2
  19. #define TFT_BL 22 // LED back-light
  20.  
  21. //int counter = 0; //rotary
  22. int vfoa = 0740.00000;
  23. int direction = DIRECTION_CW; //rotary
  24. int CLK_state; //rotary
  25. int prev_CLK_state; //rotary
  26. ezButton button(SW_PIN); // create ezButton object that attach to pin 32;
  27.  
  28. int vfob = 2400.00000;
  29. int direction2 = DIRECTION_CW; //rotary2
  30. int CLK_state2; //rotary2
  31. int prev_CLK_state2; //rotary2
  32. ezButton button2(SW_PIN2); // create ezButton object that attach to pin ;
  33.  
  34. bool updateDisplay = false;
  35.  
  36. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); // hardware SPI
  37.  
  38.  
  39. void setup(void) {
  40.  
  41. tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab
  42.  
  43. // configure encoder pins as inputs
  44. pinMode(CLK_PIN, INPUT);
  45. pinMode(DT_PIN, INPUT);
  46. button.setDebounceTime(50); // set debounce time to 50 milliseconds
  47.  
  48. // read the initial state of the rotary encoder's CLK pin
  49. prev_CLK_state = digitalRead(CLK_PIN);
  50.  
  51. // configure encoder pins as inputs
  52. pinMode(CLK_PIN2, INPUT);
  53. pinMode(DT_PIN2, INPUT);
  54. button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  55.  
  56. // read the initial state of the rotary encoder's CLK pin
  57. prev_CLK_state2 = digitalRead(CLK_PIN2);
  58.  
  59. pinMode(TFT_BL, OUTPUT);
  60. digitalWrite(TFT_BL, HIGH);
  61. tft.setRotation(1);
  62. tft.fillScreen(ST77XX_BLACK);
  63.  
  64. tft.setTextColor(ST77XX_GREEN);
  65. tft.setTextSize(2);
  66. tft.setCursor(0, 20);
  67. tft.println(vfoa);
  68. tft.println("mhz");
  69. tft.setTextColor(ST77XX_YELLOW);
  70. tft.setCursor(0, 60);
  71. tft.println(vfob);
  72. tft.println("mhz");
  73. }
  74.  
  75. void loop() {
  76. button.loop(); // MUST call the loop() function first
  77. button2.loop(); // MUST call the loop() function first
  78.  
  79. // read the current state of the rotary encoder's CLK pin
  80. CLK_state = digitalRead(CLK_PIN);
  81.  
  82. // If the state of CLK is changed, then pulse occurred
  83. // React to only the rising edge (from LOW to HIGH) to avoid double count
  84. if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
  85. // if the DT state is HIGH
  86. // the encoder is rotating in counter-clockwise direction => decrease the counter
  87. if (digitalRead(DT_PIN) == HIGH) {
  88. vfoa--;
  89. direction = DIRECTION_CCW;
  90. } else {
  91. // the encoder is rotating in clockwise direction => increase the counter
  92. vfoa++;
  93. direction = DIRECTION_CW;
  94. }
  95. updateDisplay = true;
  96. }
  97. // save last CLK state
  98. prev_CLK_state = CLK_state;
  99.  
  100. // read the current state of the rotary encoder's CLK pin
  101. CLK_state2 = digitalRead(CLK_PIN2);
  102.  
  103. // If the state of CLK is changed, then pulse occurred
  104. // React to only the rising edge (from LOW to HIGH) to avoid double count
  105. if (CLK_state2 != prev_CLK_state2 && CLK_state2 == HIGH) {
  106. // if the DT state is HIGH
  107. // the encoder is rotating in counter-clockwise direction => decrease the counter
  108. if (digitalRead(DT_PIN2) == HIGH) {
  109. vfob--;
  110. direction2 = DIRECTION_CCW;
  111. } else {
  112. // the encoder is rotating in clockwise direction => increase the counter
  113. vfob++;
  114. direction2 = DIRECTION_CW;
  115. }
  116. updateDisplay = true;
  117. }
  118. // save last CLK state
  119. prev_CLK_state2 = CLK_state2;
  120.  
  121. if (updateDisplay) {
  122. updateDisplay = false;
  123.  
  124. //to convert count to vfo and display
  125. tft.fillScreen(ST77XX_BLACK);
  126.  
  127. tft.setTextColor(ST77XX_YELLOW);
  128. tft.setCursor(0, 60);
  129. tft.println(vfob);
  130. tft.println("mhz");
  131. tft.fillScreen(ST77XX_BLACK);
  132. tft.setTextColor(ST77XX_GREEN);
  133. tft.setTextSize(2);
  134. tft.setCursor(0, 20);
  135. tft.println(vfoa);
  136. tft.println("mhz");
  137. tft.setTextColor(ST77XX_YELLOW);
  138. tft.setCursor(0, 60);
  139. tft.println(vfob);
  140. tft.println("mhz");
  141. }
  142.  
  143. if (button.isPressed()) {
  144. Serial.println("button1 is pressed");
  145. }
  146.  
  147. if (button2.isPressed()) {
  148. Serial.println("button2 is pressed");
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement