Advertisement
pleasedontcode

Encoder Display rev_07

Jul 15th, 2025
250
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:23:57
  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_KY_040_CLK_PIN_D2 = 2;
  39. const uint8_t encoder_KY_040_DT_PIN_D3 = 3;
  40. const uint8_t encoder_KY_040_SW_PIN_D4 = 4;
  41.  
  42. /***** DEFINITION OF I2C PINS *****/
  43. const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  44. const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  45. const uint8_t angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  46.  
  47. /***** LIBRARY CLASS INSTANCES *****/
  48. // Instantiate the LCD display object with the I2C address
  49. LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS);
  50.  
  51. // Instantiate the SimpleEncoder object with the encoder pins
  52. SimpleEncoder encoder(encoder_KY_040_SW_PIN_D4, encoder_KY_040_CLK_PIN_D2, encoder_KY_040_DT_PIN_D3);
  53.  
  54. void setup(void)
  55. {
  56.   // Initialize serial communication for debugging (optional)
  57.   Serial.begin(9600);
  58.  
  59.   // Initialize the LCD display
  60.   if (lcd.begin()) {
  61.     lcd.clear();
  62.     lcd.setBacklight(1); // Turn on backlight
  63.     lcd.setCursor(0, 0);
  64.     lcd.print("Rotational Angle");
  65.   }
  66.  
  67.   // Configure encoder pins
  68.   pinMode(encoder_KY_040_CLK_PIN_D2, INPUT);
  69.   pinMode(encoder_KY_040_DT_PIN_D3, INPUT);
  70.   pinMode(encoder_KY_040_SW_PIN_D4, INPUT_PULLUP);
  71. }
  72.  
  73. void loop(void)
  74. {
  75.   // Update the encoder state
  76.   encoder.tick();
  77.  
  78.   // Calculate the angle in degrees based on the encoder's count
  79.   // Assuming the encoder count ranges from 0 to 360 for a full rotation
  80.   // If the encoder count is not in degrees, convert accordingly
  81.   float angleDegrees = encoder.VALUE; // The encoder.VALUE provides the current count
  82.  
  83.   // Display the angle on the LCD
  84.   lcd.setCursor(0, 1);
  85.   lcd.print("Angle: ");
  86.   lcd.print(angleDegrees);
  87.   lcd.print(" deg   "); // Extra spaces to clear previous longer values
  88.  
  89.   // Optional: Print to Serial for debugging
  90.   Serial.print("Encoder Count: ");
  91.   Serial.println(angleDegrees);
  92.  
  93.   // Small delay to prevent flickering
  94.   delay(100);
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement