Advertisement
pleasedontcode

Encoder Display rev_08

Jul 15th, 2025
262
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 23:24:27
  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. // Corrected variable names: replace hyphens with underscores
  39. const uint8_t encoder_KY040_CLK_PIN_D2 = 2;
  40. const uint8_t encoder_KY040_DT_PIN_D3 = 3;
  41. const uint8_t encoder_KY040_SW_PIN_D4 = 4;
  42.  
  43. /***** DEFINITION OF I2C PINS *****/
  44. // Not used directly in code; default I2C pins for ESP32 are used
  45. const uint8_t angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  46.  
  47. /****** LIBRARY CLASS INSTANCES *****/
  48. SimpleEncoder encoder(encoder_KY040_CLK_PIN_D2, encoder_KY040_DT_PIN_D3, encoder_KY040_SW_PIN_D4);
  49. LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  50.  
  51. void setup(void)
  52. {
  53.   // Initialize LCD
  54.   if (lcd.begin()) {
  55.     lcd.print("Encoder Angle:");
  56.   }
  57.   // Set pin modes for encoder pins
  58.   pinMode(encoder_KY040_CLK_PIN_D2, INPUT);
  59.   pinMode(encoder_KY040_DT_PIN_D3, INPUT);
  60.   pinMode(encoder_KY040_SW_PIN_D4, INPUT_PULLUP);
  61. }
  62.  
  63. void loop(void)
  64. {
  65.   // Read the encoder angle in degrees
  66.   float angleDegrees = encoder.getValue(); // getValue() returns the angle in degrees
  67.  
  68.   // Display the angle on the LCD
  69.   lcd.setCursor(0, 1);
  70.   lcd.print("Angle: ");
  71.   lcd.print(angleDegrees);
  72.   lcd.print(" deg   "); // Extra spaces to clear previous longer values
  73.  
  74.   delay(200); // Small delay for stability
  75. }
  76.  
  77. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement