pleasedontcode

Encoder Display rev_06

Jul 15th, 2025
191
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:22:17
  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. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <SimpleEncoder.h> // http://github.com/EasyG0ing1/SimpleEncoder
  30. #include <LCDIC2.h>        // https://github.com/offcircuit/LCDIC2
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. // Corrected pin naming to avoid invalid characters
  38. const uint8_t encoder_KY040_CLK_PIN_D2 = 2;
  39. const uint8_t encoder_KY040_DT_PIN_D3 = 3;
  40. const uint8_t encoder_KY040_SW_PIN_D4 = 4;
  41.  
  42. /***** DEFINITION OF I2C PINS *****/
  43. const uint8_t angleDisplay_LCD1602I2C_SDA = A4;
  44. const uint8_t angleDisplay_LCD1602I2C_SCL = A5;
  45. const uint8_t angleDisplay_LCD1602I2C_SLAVE_ADDRESS = 39;
  46.  
  47. /***** LIBRARY CLASS INSTANCES *****/
  48. // Instantiate the encoder object with the pins
  49. SimpleEncoder encoder(encoder_KY040_CLK_PIN_D2, encoder_KY040_DT_PIN_D3, encoder_KY040_SW_PIN_D4);
  50.  
  51. // Instantiate the LCD object with the I2C address and dimensions
  52. LCDIC2 lcd(angleDisplay_LCD1602I2C_SLAVE_ADDRESS, 16, 2);
  53.  
  54. void setup(void)
  55. {
  56.   // Initialize serial communication for debugging
  57.   Serial.begin(9600);
  58.  
  59.   // Initialize the LCD
  60.   if (lcd.begin()) {
  61.     lcd.clear();
  62.     lcd.print("Angle:");
  63.   }
  64.  
  65.   // Configure encoder pins
  66.   pinMode(encoder_KY040_CLK_PIN_D2, INPUT);
  67.   pinMode(encoder_KY040_DT_PIN_D3, INPUT);
  68.   pinMode(encoder_KY040_SW_PIN_D4, INPUT_PULLUP);
  69. }
  70.  
  71. void loop(void)
  72. {
  73.   // Read the encoder's rotational angle in degrees
  74.   float angleDegrees = encoder.getValue() * 360.0; // Assuming getValue() returns number of steps
  75.  
  76.   // Normalize the angle to 0-360 degrees
  77.   if (angleDegrees < 0) {
  78.     angleDegrees += 360.0;
  79.   } else if (angleDegrees >= 360.0) {
  80.     angleDegrees -= 360.0;
  81.   }
  82.  
  83.   // Display the angle on the LCD
  84.   lcd.setCursor(0, 1);
  85.   lcd.print("Angle:");
  86.   lcd.print(angleDegrees, 1); // One decimal place
  87.   lcd.print((char)223); // Degree symbol
  88.   lcd.print(" ");
  89.  
  90.   // Optional: print to serial for debugging
  91.   Serial.print("Angle: ");
  92.   Serial.print(angleDegrees, 1);
  93.   Serial.println(" degrees");
  94.  
  95.   delay(100); // Small delay for stability
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment