Advertisement
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 pin GPIO25 connected to the rotary encoder's CLK pin
- #define DT_PIN 35 // ESP32 pin GPIO26 connected to the rotary encoder's DT pin
- #define SW_PIN 32 // ESP32 pin GPIO27 connected to the rotary encoder's SW pin
- #define CLK_PIN2 33 // ESP32 2nd connected rotary encoder's CLK pin
- #define DT_PIN2 25 // ESP32 2nd connected rotary encoder's DT pin
- #define SW_PIN2 13 // ESP32 2nd connected rotary encoder's SW pin
- #define DIRECTION_CW 0 // clockwise direction rotary
- #define DIRECTION_CCW 1 // counter-clockwise direction rotary
- #define TFT_CS 5
- #define TFT_RST 4
- #define TFT_DC 2
- #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 32;
- int vfob = 2400.00000;
- int direction2 = DIRECTION_CW; //rotary2
- int CLK_state2; //rotary2
- int prev_CLK_state2; //rotary2
- ezButton button2(SW_PIN2); // create ezButton object that attach to pin ;
- bool updateDisplay = false;
- 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);
- // configure encoder pins as inputs
- pinMode(CLK_PIN2, INPUT);
- pinMode(DT_PIN2, INPUT);
- button2.setDebounceTime(50); // set debounce time to 50 milliseconds
- // read the initial state of the rotary encoder's CLK pin
- prev_CLK_state2 = digitalRead(CLK_PIN2);
- 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
- button2.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 {
- // the encoder is rotating in clockwise direction => increase the counter
- vfoa++;
- direction = DIRECTION_CW;
- }
- updateDisplay = true;
- }
- // save last CLK state
- prev_CLK_state = CLK_state;
- // read the current state of the rotary encoder's CLK pin
- CLK_state2 = digitalRead(CLK_PIN2);
- // 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_CCW;
- } else {
- // the encoder is rotating in clockwise direction => increase the counter
- vfob++;
- direction2 = DIRECTION_CW;
- }
- updateDisplay = true;
- }
- // save last CLK state
- prev_CLK_state2 = CLK_state2;
- if (updateDisplay) {
- updateDisplay = false;
- //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");
- 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");
- }
- if (button.isPressed()) {
- Serial.println("button1 is pressed");
- }
- if (button2.isPressed()) {
- Serial.println("button2 is pressed");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement