#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
// Declare LCD object
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Declare rotary encoder object
Encoder myEnc(3, 2);
// Declare variables to keep track of the cursor position
int cursorX = 0;
int cursorY = 0;
int previousEncoderValue = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
// Print some initial text on the LCD
lcd.print("Welcome to Arduino");
lcd.setCursor(0, 1);
lcd.print("Scrolling Display");
lcd.setCursor(0, 2);
lcd.print("Using Rotary Encoder");
}
void loop() {
// Read the value of the rotary encoder
long encoderValue = myEnc.read();
// Check if the encoder has been turned
if (encoderValue != previousEncoderValue) {
// Check if the encoder has been turned up
if (encoderValue > previousEncoderValue) {
cursorY--;
// Check if the cursor has gone past the top of the screen
if (cursorY < 0) {
cursorY = 3;
}
} else {
// Encoder has been turned down
cursorY++;
// Check if the cursor has gone past the bottom of the screen
if (cursorY > 3) {
cursorY = 0;
}
}
lcd.setCursor(cursorX, cursorY);
previousEncoderValue = encoderValue;
}
}