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:23:57
- ********* 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_KY_040_CLK_PIN_D2 = 2;
- const uint8_t encoder_KY_040_DT_PIN_D3 = 3;
- const uint8_t encoder_KY_040_SW_PIN_D4 = 4;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
- const uint8_t angleDisplay_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
- const uint8_t angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
- /***** LIBRARY CLASS INSTANCES *****/
- // Instantiate the LCD display object with the I2C address
- LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS);
- // Instantiate the SimpleEncoder object with the encoder pins
- SimpleEncoder encoder(encoder_KY_040_SW_PIN_D4, encoder_KY_040_CLK_PIN_D2, encoder_KY_040_DT_PIN_D3);
- void setup(void)
- {
- // Initialize serial communication for debugging (optional)
- Serial.begin(9600);
- // Initialize the LCD display
- if (lcd.begin()) {
- lcd.clear();
- lcd.setBacklight(1); // Turn on backlight
- lcd.setCursor(0, 0);
- lcd.print("Rotational Angle");
- }
- // Configure encoder pins
- pinMode(encoder_KY_040_CLK_PIN_D2, INPUT);
- pinMode(encoder_KY_040_DT_PIN_D3, INPUT);
- pinMode(encoder_KY_040_SW_PIN_D4, INPUT_PULLUP);
- }
- void loop(void)
- {
- // Update the encoder state
- encoder.tick();
- // Calculate the angle in degrees based on the encoder's count
- // Assuming the encoder count ranges from 0 to 360 for a full rotation
- // If the encoder count is not in degrees, convert accordingly
- float angleDegrees = encoder.VALUE; // The encoder.VALUE provides the current count
- // 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
- // Optional: Print to Serial for debugging
- Serial.print("Encoder Count: ");
- Serial.println(angleDegrees);
- // Small delay to prevent flickering
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement