DuboisP

ESP32 LCD1602 Rotary Encoder

Feb 2nd, 2025 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | Source Code | 0 0
  1. /*********
  2.    title    :  ESP32 LCD2024 Rotary Encoder
  3.    target   :  ESP32 Dev Module
  4.                ESP32 pinout
  5.                LCD2004_I2C display SDA on GPIO21, SCL on GPIO22
  6.                
  7.    
  8.    author   :  Patrick Dubois
  9.    date     :  2025-02-02
  10.    license  :  public domain
  11.                  
  12. *********/
  13.  
  14. #include <LiquidCrystal_I2C.h>
  15. #include <Wire.h>
  16.  
  17. // --- Configuration de l'écran LCD ---
  18. #define numCols   20                               // for lcd
  19. #define numRows    4
  20. #define SCREEN_ADDRESS 0x27  
  21. LiquidCrystal_I2C lcd(SCREEN_ADDRESS, numCols, numRows);     // use this for I2C LCD.
  22.  
  23. #define baudrateSerial  115200                     // vitesse communication Arduino - PC  
  24.  
  25. #define button0Pin      0        // GPIO 0 or PRG button
  26. #define pinEncoderClick 25
  27. #define pinData         26
  28. #define pinClock        27
  29. #define tempoClick      50       //  50 ms
  30. #define tempoRotation   100      // 100 ms
  31. volatile int16_t counter = 0;
  32. uint32_t oldMillis, oldMillisR, newMillis;
  33. bool bRotaryStatus = false, bCounterStatus = false;
  34. uint8_t npinDataValue, oldnpinDataValue = 2;
  35. String RotationWay[] = {"Right", "Left", "None"};
  36.  
  37. // void setup() est après void loop() :-)
  38.  
  39. void IRAM_ATTR onButton0Event() {
  40.    counter = 0;
  41.    npinDataValue = oldnpinDataValue = 2;
  42.    bCounterStatus = bRotaryStatus = true;
  43. }
  44.  
  45. void IRAM_ATTR onEncoderClickEvent() {
  46.    newMillis = millis();
  47.    if (newMillis > oldMillis + tempoClick) {
  48.       ++counter;
  49.       oldMillis = newMillis;
  50.       bCounterStatus = true;
  51.    }
  52. }
  53.  
  54. void IRAM_ATTR onRotaryEvent() {
  55.    newMillis = millis();
  56.    if (newMillis > oldMillisR + tempoRotation) {
  57.       npinDataValue = digitalRead(pinData);
  58.       oldMillisR = newMillis;
  59.       bRotaryStatus = true;
  60.    }
  61. }
  62.  
  63. void loop() {
  64.    char counterbuffer[4];
  65.    char buffer[64];
  66.  
  67.    if (bCounterStatus) {
  68.       sprintf(counterbuffer, "%3d", counter);
  69.       lcd.setCursor(12, 0);
  70.       lcd.print(counterbuffer);
  71.       bCounterStatus = false;
  72.    }
  73.  
  74.    if (bRotaryStatus) {
  75.       sprintf(buffer,"%s to %s  ", RotationWay[oldnpinDataValue], RotationWay[npinDataValue]);
  76.       lcd.setCursor(0, 3);
  77.       lcd.print(buffer);
  78.       //Serial.println(npinDataValue);
  79.       oldnpinDataValue = npinDataValue;
  80.       bRotaryStatus = false;
  81.    }
  82. }
  83.  
  84. void setup() {
  85.    Serial.begin(baudrateSerial);
  86.  
  87.    pinMode(pinEncoderClick, INPUT_PULLUP);
  88.    pinMode(pinClock, INPUT);
  89.    pinMode(pinData, INPUT);
  90.    pinMode(button0Pin, INPUT_PULLDOWN);                     // GPIO 0
  91.    attachInterrupt(digitalPinToInterrupt(pinEncoderClick), onEncoderClickEvent, RISING);    // gestion du bouton par interruption
  92.    attachInterrupt(digitalPinToInterrupt(pinClock), onRotaryEvent, FALLING);    // gestion de la rotation par interruption
  93.    attachInterrupt(digitalPinToInterrupt(button0Pin), onButton0Event, RISING);
  94.  
  95.    newMillis = oldMillis = oldMillisR = millis();
  96.  
  97.    // Initialisation de l'écran LCD
  98.    lcd.init();
  99.    lcd.backlight();
  100.    lcd.setCursor(0, 0);
  101.    lcd.print("Init Display...");
  102.    delay(1000);  
  103.    lcd.clear();
  104.    lcd.setCursor(0, 0); lcd.print("Clics");
  105.    lcd.setCursor(0, 2); lcd.print("Rotation Status");
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment