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:09:10
- ********* 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 LCD object with I2C address
- LCDIC2 lcd(angleDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
- // Instantiate encoder object
- SimpleEncoder encoder(encoder_KY_040_SW_PIN_D4, encoder_KY_040_CLK_PIN_D2, encoder_KY_040_DT_PIN_D3);
- // Variable to store the angle in degrees
- float angleDegrees = 0.0;
- void setup(void)
- {
- // put your setup code here, to run once:
- 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);
- // Initialize LCD
- lcd.begin();
- // Display initial message
- lcd.clear();
- lcd.print("Angle: 0.0");
- }
- void loop(void)
- {
- // Update encoder state
- encoder.adjustValue();
- // Calculate the angle in degrees based on encoder value
- // Assuming encoder value ranges from 0 to 360 for full rotation
- // Adjust as per your encoder's resolution and value range
- angleDegrees = (float)encoder.VALUE;
- // Display the angle on LCD
- lcd.setCursor(0, 0);
- lcd.print("Angle: ");
- lcd.print(angleDegrees);
- lcd.print(" deg");
- // Small delay to avoid flickering
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment