pleasedontcode

Encoder Display rev_05

Jul 15th, 2025
177
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:09:10
  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. const uint8_t encoder_KY_040_CLK_PIN_D2 = 2;
  38. const uint8_t encoder_KY_040_DT_PIN_D3 = 3;
  39. const uint8_t encoder_KY_040_SW_PIN_D4 = 4;
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  43. const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  44. const uint8_t angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  45.  
  46. /****** LIBRARY CLASS INSTANCES *****/
  47. // Instantiate LCD object with I2C address
  48. LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  49. // Instantiate encoder object
  50. SimpleEncoder encoder(encoder_KY_040_SW_PIN_D4, encoder_KY_040_CLK_PIN_D2, encoder_KY_040_DT_PIN_D3);
  51. // Variable to store the angle in degrees
  52. float angleDegrees = 0.0;
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.     pinMode(encoder_KY_040_CLK_PIN_D2, INPUT);
  58.     pinMode(encoder_KY_040_DT_PIN_D3, INPUT);
  59.     pinMode(encoder_KY_040_SW_PIN_D4, INPUT_PULLUP);
  60.     // Initialize LCD
  61.     lcd.begin();
  62.     // Display initial message
  63.     lcd.clear();
  64.     lcd.print("Angle: 0.0");
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // Update encoder state
  70.     encoder.adjustValue();
  71.     // Calculate the angle in degrees based on encoder value
  72.     // Assuming encoder value ranges from 0 to 360 for full rotation
  73.     // Adjust as per your encoder's resolution and value range
  74.     angleDegrees = (float)encoder.VALUE;
  75.     // Display the angle on LCD
  76.     lcd.setCursor(0, 0);
  77.     lcd.print("Angle: ");
  78.     lcd.print(angleDegrees);
  79.     lcd.print(" deg");
  80.     // Small delay to avoid flickering
  81.     delay(100);
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment