Advertisement
DuboisP

ESP32 LCD1602 Rotary Encoder

Feb 2nd, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | Source Code | 0 0
  1. /*********
  2. title : ESP32 LCD1602 Rotary Encoder
  3. target : ESP32 Dev Module
  4. ESP32 pinout
  5. LCD1602_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 pinEncoderClick 25
  26. #define pinData 26
  27. #define pinClock 27
  28. #define tempoClick 50 // 50 ms
  29. #define tempoRotation 100 // 100 ms
  30. volatile int16_t counter = 0;
  31. uint32_t oldMillis, oldMillisR, newMillis;
  32. bool bRotaryStatus = false, bCounterStatus = false;
  33. uint8_t npinDataValue, oldnpinDataValue = 2;
  34. String RotationWay[] = {"Right", "Left", "None"};
  35.  
  36. // void setup() est après void loop() :-)
  37.  
  38. void IRAM_ATTR onEncoderClickEvent() {
  39. newMillis = millis();
  40. if (newMillis > oldMillis + tempoClick) {
  41. ++counter;
  42. oldMillis = newMillis;
  43. bCounterStatus = true;
  44. }
  45. }
  46.  
  47. void IRAM_ATTR onRotaryEvent() {
  48. newMillis = millis();
  49. if (newMillis > oldMillisR + tempoRotation) {
  50. npinDataValue = digitalRead(pinData);
  51. oldMillisR = newMillis;
  52. bRotaryStatus = true;
  53. }
  54. }
  55.  
  56. void loop() {
  57. char buffer[64];
  58.  
  59. if (bCounterStatus) {
  60. lcd.setCursor(13, 0);
  61. lcd.print(counter);
  62. bCounterStatus = false;
  63. }
  64.  
  65. if (bRotaryStatus) {
  66. sprintf(buffer,"from %s to %s ", RotationWay[oldnpinDataValue], RotationWay[npinDataValue]);
  67. lcd.setCursor(0, 3);
  68. lcd.print(buffer);
  69. //Serial.println(npinDataValue);
  70. oldnpinDataValue = npinDataValue;
  71. bRotaryStatus = false;
  72. }
  73. }
  74.  
  75. void setup() {
  76. Serial.begin(baudrateSerial);
  77.  
  78. // Initialisation de l'écran LCD
  79. lcd.init();
  80. lcd.backlight();
  81. lcd.setCursor(0, 0);
  82. lcd.print("Init Display...");
  83. delay(1000);
  84. lcd.clear();
  85.  
  86. pinMode(pinEncoderClick, INPUT_PULLUP);
  87. pinMode(pinClock, INPUT);
  88. pinMode(pinData, INPUT);
  89. attachInterrupt(digitalPinToInterrupt(pinEncoderClick), onEncoderClickEvent, RISING); // gestion du bouton par interruption
  90. attachInterrupt(digitalPinToInterrupt(pinClock), onRotaryEvent, FALLING); // gestion du bouton par interruption
  91. newMillis = oldMillis = oldMillisR = millis();
  92.  
  93. lcd.setCursor(0, 0); lcd.print("Clics");
  94. lcd.setCursor(0, 1); lcd.print("Rotation Status");
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement