Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_GFX.h>
- #include <Adafruit_ST7735.h>
- #include <ezButton.h> // the library to use for SW pin on encoder
- #define CLK_PIN 34 // ESP32 connected to the rotary encoder's CLK pin
- #define DT_PIN 35 // ESP32 connected to the rotary encoder's DT pin
- #define SW_PIN 32 // ESP32 connected to the rotary encoder's SW pin
- #define CLK_PIN2 4 // ESP32 2nd connected rotary encoder's CLK pin
- #define DT_PIN2 16 // ESP32 2nd connected rotary encoder's DT pin
- #define SW_PIN2 17 // ESP32 2nd connected rotary encoder's SW pin
- #define DIRECTION_CW 0 // clockwise direction rotary2
- #define DIRECTION_CCW 1 // counter-clockwise direction rotary2
- #define DIRECTION_CW2 0 // clockwise direction rotary2
- #define DIRECTION_CCW2 1 // counter-clockwise direction rotary2
- #define TFT_CS 5
- #define TFT_RST 2
- #define TFT_DC 0
- //#define vfoa 0740.00000
- //#define vfob 2400.00000
- #define TFT_BL 22 // LED back-light
- //int counter = 0; //rotary
- int vfoa = 0740.00000;
- int direction = DIRECTION_CW; //rotary
- int CLK_state; //rotary
- int prev_CLK_state; //rotary
- ezButton button(SW_PIN); // create ezButton object that attach to pin ;
- int vfob = 2400.00000;
- int direction2 = DIRECTION_CW2; //rotary2
- int CLK_state2; //rotary2
- int prev_CLK_state2; //rotary2
- //ezButton button(SW_PIN2); // create ezButton object that attach to pin ;
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); // hardware SPI
- void setup(void) {
- tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab
- // configure encoder pins as inputs
- pinMode(CLK_PIN, INPUT);
- pinMode(DT_PIN, INPUT);
- button.setDebounceTime(50); // set debounce time to 50 milliseconds
- // read the initial state of the rotary encoder's CLK pin
- prev_CLK_state = digitalRead(CLK_PIN);
- pinMode(CLK_PIN2, INPUT);
- pinMode(DT_PIN2, INPUT);
- button.setDebounceTime(50); // set debounce time to 50 milliseconds
- // read the initial state of the rotary encoder's CLK pin
- prev_CLK_state2 = digitalRead(CLK_PIN);
- pinMode(TFT_BL, OUTPUT);
- digitalWrite(TFT_BL, HIGH);
- tft.setRotation(1);
- tft.fillScreen(ST77XX_BLACK);
- tft.setTextColor(ST77XX_GREEN);
- tft.setTextSize(2);
- tft.setCursor(0, 20);
- tft.println(vfoa);
- tft.println("mhz");
- tft.setTextColor(ST77XX_YELLOW);
- tft.setCursor(0, 60);
- tft.println(vfob);
- tft.println("mhz");
- }
- void loop() {
- button.loop(); // MUST call the loop() function first
- // read the current state of the rotary encoder's CLK pin
- CLK_state = digitalRead(CLK_PIN);
- // If the state of CLK is changed, then pulse occurred
- // React to only the rising edge (from LOW to HIGH) to avoid double count
- if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
- // if the DT state is HIGH
- // the encoder is rotating in counter-clockwise direction => decrease the counter
- if (digitalRead(DT_PIN) == HIGH) {
- vfoa--;
- direction = DIRECTION_CCW;
- } else {
- vfoa++;
- direction = DIRECTION_CW;
- button.loop(); // MUST call the loop() function first
- // read the current state of the rotary encoder's CLK pin
- CLK_state2 = digitalRead(CLK_PIN);
- // If the state of CLK is changed, then pulse occurred
- // React to only the rising edge (from LOW to HIGH) to avoid double count
- if (CLK_state2 != prev_CLK_state2 && CLK_state2 == HIGH) {
- // if the DT state is HIGH
- // the encoder is rotating in counter-clockwise direction => decrease the counter
- if (digitalRead(DT_PIN2) == HIGH) {
- vfob--;
- direction2 = DIRECTION_CCW2;
- } else {
- // the encoder is rotating in clockwise direction => increase the counter
- vfob++;
- direction = DIRECTION_CW2;
- }
- //to convert count to vfo and display
- tft.fillScreen(ST77XX_BLACK);
- tft.setTextColor(ST77XX_YELLOW);
- tft.setCursor(0, 60);
- tft.println(vfob);
- tft.println("mhz");
- //Serial.print("Rotary Encoder:: direction: ");
- //if (direction == DIRECTION_CW)
- // Serial.print("Clockwise");
- // else;
- tft.fillScreen(ST77XX_BLACK);
- tft.setTextColor(ST77XX_GREEN);
- tft.setTextSize(2);
- tft.setCursor(0, 20);
- tft.println(vfoa);
- tft.println("mhz");
- tft.setTextColor(ST77XX_YELLOW);
- tft.setCursor(0, 60);
- tft.println(vfob);
- tft.println("mhz");
- // Serial.print("Counter-clockwise");
- // Serial.print(" - count: ");
- //Serial.println(counter);
- }
- // save last CLK state
- prev_CLK_state = CLK_state;
- //if (button.isPressed()) {
- // Serial.println("The button is pressed");
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment