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:22:17
- ********* 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 pin naming to avoid invalid characters
- 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 *****/
- const uint8_t angleDisplay_LCD1602I2C_SDA = A4;
- const uint8_t angleDisplay_LCD1602I2C_SCL = A5;
- const uint8_t angleDisplay_LCD1602I2C_SLAVE_ADDRESS = 39;
- /***** LIBRARY CLASS INSTANCES *****/
- // Instantiate the encoder object with the pins
- SimpleEncoder encoder(encoder_KY040_CLK_PIN_D2, encoder_KY040_DT_PIN_D3, encoder_KY040_SW_PIN_D4);
- // Instantiate the LCD object with the I2C address and dimensions
- LCDIC2 lcd(angleDisplay_LCD1602I2C_SLAVE_ADDRESS, 16, 2);
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(9600);
- // Initialize the LCD
- if (lcd.begin()) {
- lcd.clear();
- lcd.print("Angle:");
- }
- // Configure 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's rotational angle in degrees
- float angleDegrees = encoder.getValue() * 360.0; // Assuming getValue() returns number of steps
- // Normalize the angle to 0-360 degrees
- if (angleDegrees < 0) {
- angleDegrees += 360.0;
- } else if (angleDegrees >= 360.0) {
- angleDegrees -= 360.0;
- }
- // Display the angle on the LCD
- lcd.setCursor(0, 1);
- lcd.print("Angle:");
- lcd.print(angleDegrees, 1); // One decimal place
- lcd.print((char)223); // Degree symbol
- lcd.print(" ");
- // Optional: print to serial for debugging
- Serial.print("Angle: ");
- Serial.print(angleDegrees, 1);
- Serial.println(" degrees");
- delay(100); // Small delay for stability
- }
Advertisement
Add Comment
Please, Sign In to add comment