Guest User

Untitled

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