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 22:52:12
- ********* 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 *****/
- const uint8_t encoder_CLK_PIN = 2;
- const uint8_t encoder_DT_PIN = 3;
- const uint8_t encoder_SW_PIN = 4;
- /***** DEFINITION OF I2C PINS *****/
- // Note: On ESP32, the default I2C pins are typically GPIO 21 (SDA) and GPIO 22 (SCL),
- // but here we define A4 and A5 for compatibility. Ensure these are used correctly.
- const uint8_t LCD_SDA_PIN = A4;
- const uint8_t LCD_SCL_PIN = A5;
- const uint8_t LCD_I2C_ADDRESS = 0x27; // Common address for LCD1602 I2C
- /***** LIBRARIES CLASS INSTANCES *****/
- // Instantiate the LCD
- LCDIC2 lcd(LCD_I2C_ADDRESS, 16, 2);
- // Instantiate the encoder
- SimpleEncoder encoder(encoder_CLK_PIN, encoder_DT_PIN, encoder_SW_PIN);
- void setup(void)
- {
- // Initialize serial communication for debugging (optional)
- Serial.begin(9600);
- // Initialize LCD
- if (lcd.begin()) {
- lcd.print("Angle:");
- }
- // Configure encoder pins
- pinMode(encoder_CLK_PIN, INPUT);
- pinMode(encoder_DT_PIN, INPUT);
- pinMode(encoder_SW_PIN, INPUT_PULLUP);
- }
- void loop(void)
- {
- static long lastReportedAngle = 0;
- // Update the encoder state
- encoder.check();
- // Read the current angle in degrees
- float angle = encoder.ANGLE;
- // Optional: Only update display if angle changes significantly
- if (abs(angle - lastReportedAngle) >= 1) {
- lastReportedAngle = angle;
- // Clear the previous angle display
- lcd.setCursor(0, 1);
- lcd.print(" "); // Clear line
- lcd.setCursor(0, 1);
- lcd.print("Angle: ");
- lcd.print(angle);
- lcd.print(" deg");
- // Also print to serial for debugging
- Serial.print("Rotational Angle: ");
- Serial.print(angle);
- Serial.println(" degrees");
- }
- delay(100); // Small delay to debounce and reduce flickering
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment