pleasedontcode

Encoder Display rev_04

Jul 15th, 2025
199
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:52
  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. /****** DECLARATION OF LIBRARY OBJECT INSTANCES *****/
  48. // Initialize SimpleEncoder with CLK, DT, SW pins
  49. SimpleEncoder encoder(encoder_KY_040_CLK_PIN_D2, encoder_KY_040_DT_PIN_D3, encoder_KY_040_SW_PIN_D4);
  50.  
  51. // Initialize LCD with I2C address, 16 columns, 2 rows
  52. LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  53.  
  54. /****** VARIABLE DECLARATION *****/
  55. float angleDegrees = 0;
  56.  
  57. void setup(void)
  58. {
  59.     // Initialize serial communication for debugging (optional)
  60.     Serial.begin(9600);
  61.  
  62.     // Setup encoders pins
  63.     pinMode(encoder_KY_040_CLK_PIN_D2, INPUT);
  64.     pinMode(encoder_KY_040_DT_PIN_D3, INPUT);
  65.     pinMode(encoder_KY_040_SW_PIN_D4, INPUT_PULLUP);
  66.  
  67.     // Initialize LCD
  68.     if (lcd.begin()) {
  69.         lcd.clear();
  70.         lcd.setCursor(0, 0);
  71.         lcd.print("Rotary Encoder");
  72.         lcd.setCursor(0, 1);
  73.         lcd.print("Angle: 0 deg");
  74.     }
  75. }
  76.  
  77. void loop(void)
  78. {
  79.     // Read encoder for clockwise rotation
  80.     if (encoder.CLOCKWISE)
  81.     {
  82.         angleDegrees += 1;
  83.         encoder.CLOCKWISE = false; // Reset the flag
  84.     }
  85.     // Read encoder for counterclockwise rotation
  86.     if (encoder.COUNTERCLOCKWISE)
  87.     {
  88.         angleDegrees -= 1;
  89.         encoder.COUNTERCLOCKWISE = false; // Reset the flag
  90.     }
  91.     // Optional: Clamp angle between 0 and 360
  92.     if (angleDegrees >= 360)
  93.     {
  94.         angleDegrees -= 360;
  95.     }
  96.     else if (angleDegrees < 0)
  97.     {
  98.         angleDegrees += 360;
  99.     }
  100.  
  101.     // Display the angle on the LCD
  102.     lcd.setCursor(0, 1);
  103.     lcd.print("Angle: ");
  104.     lcd.print(angleDegrees);
  105.     lcd.print(" deg  ");
  106.  
  107.     // Optional: Print to serial for debugging
  108.     Serial.print("Current angle: ");
  109.     Serial.println(angleDegrees);
  110.  
  111.     // Small delay to improve readability and debounce
  112.     delay(100);
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment