Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Encoder Display
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-07-15 23:24:27
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Capture the rotational angle in degrees from the */
- /* KY-040 rotary encoder and show the result on an */
- /* LCD screen through I2C communication, leveraging */
- /* the SimpleEncoder library. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <SimpleEncoder.h> // http://github.com/EasyG0ing1/SimpleEncoder
- #include <LCDIC2.h> // https://github.com/offcircuit/LCDIC2
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- // Corrected variable names: replace hyphens with underscores
- const uint8_t encoder_KY040_CLK_PIN_D2 = 2;
- const uint8_t encoder_KY040_DT_PIN_D3 = 3;
- const uint8_t encoder_KY040_SW_PIN_D4 = 4;
- /***** DEFINITION OF I2C PINS *****/
- // Not used directly in code; default I2C pins for ESP32 are used
- const uint8_t angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
- /****** LIBRARY CLASS INSTANCES *****/
- SimpleEncoder encoder(encoder_KY040_CLK_PIN_D2, encoder_KY040_DT_PIN_D3, encoder_KY040_SW_PIN_D4);
- LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
- void setup(void)
- {
- // Initialize LCD
- if (lcd.begin()) {
- lcd.print("Encoder Angle:");
- }
- // Set pin modes for encoder pins
- pinMode(encoder_KY040_CLK_PIN_D2, INPUT);
- pinMode(encoder_KY040_DT_PIN_D3, INPUT);
- pinMode(encoder_KY040_SW_PIN_D4, INPUT_PULLUP);
- }
- void loop(void)
- {
- // Read the encoder angle in degrees
- float angleDegrees = encoder.getValue(); // getValue() returns the angle in degrees
- // Display the angle on the LCD
- lcd.setCursor(0, 1);
- lcd.print("Angle: ");
- lcd.print(angleDegrees);
- lcd.print(" deg "); // Extra spaces to clear previous longer values
- delay(200); // Small delay for stability
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement