pleasedontcode

Encoder Display rev_03

Jul 15th, 2025
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Encoder Display
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-07-15 22:52:12
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Capture the rotational angle in degrees from the */
  21.     /* KY-040 rotary encoder and show the result on an */
  22.     /* LCD screen through I2C communication, leveraging */
  23.     /* the SimpleEncoder library. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <SimpleEncoder.h>    //http://github.com/EasyG0ing1/SimpleEncoder
  31. #include <LCDIC2.h>           //https://github.com/offcircuit/LCDIC2
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t encoder_CLK_PIN = 2;
  39. const uint8_t encoder_DT_PIN = 3;
  40. const uint8_t encoder_SW_PIN = 4;
  41.  
  42. /***** DEFINITION OF I2C PINS *****/
  43. // Note: On ESP32, the default I2C pins are typically GPIO 21 (SDA) and GPIO 22 (SCL),
  44. // but here we define A4 and A5 for compatibility. Ensure these are used correctly.
  45. const uint8_t LCD_SDA_PIN = A4;
  46. const uint8_t LCD_SCL_PIN = A5;
  47. const uint8_t LCD_I2C_ADDRESS = 0x27; // Common address for LCD1602 I2C
  48.  
  49. /***** LIBRARIES CLASS INSTANCES *****/
  50. // Instantiate the LCD
  51. LCDIC2 lcd(LCD_I2C_ADDRESS, 16, 2);
  52.  
  53. // Instantiate the encoder
  54. SimpleEncoder encoder(encoder_CLK_PIN, encoder_DT_PIN, encoder_SW_PIN);
  55.  
  56. void setup(void)
  57. {
  58.     // Initialize serial communication for debugging (optional)
  59.     Serial.begin(9600);
  60.  
  61.     // Initialize LCD
  62.     if (lcd.begin()) {
  63.         lcd.print("Angle:");
  64.     }
  65.  
  66.     // Configure encoder pins
  67.     pinMode(encoder_CLK_PIN, INPUT);
  68.     pinMode(encoder_DT_PIN, INPUT);
  69.     pinMode(encoder_SW_PIN, INPUT_PULLUP);
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     static long lastReportedAngle = 0;
  75.     // Update the encoder state
  76.     encoder.check();
  77.  
  78.     // Read the current angle in degrees
  79.     float angle = encoder.ANGLE;
  80.  
  81.     // Optional: Only update display if angle changes significantly
  82.     if (abs(angle - lastReportedAngle) >= 1) {
  83.         lastReportedAngle = angle;
  84.  
  85.         // Clear the previous angle display
  86.         lcd.setCursor(0, 1);
  87.         lcd.print("                "); // Clear line
  88.         lcd.setCursor(0, 1);
  89.         lcd.print("Angle: ");
  90.         lcd.print(angle);
  91.         lcd.print(" deg");
  92.  
  93.         // Also print to serial for debugging
  94.         Serial.print("Rotational Angle: ");
  95.         Serial.print(angle);
  96.         Serial.println(" degrees");
  97.     }
  98.  
  99.     delay(100); // Small delay to debounce and reduce flickering
  100. }
  101.  
  102. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment