#include <LiquidCrystal.h>
// Define the number of characters on each row
#define numCharacters 16
// Define the scroll delay variables
int i = 0;
long prevTime = 0;
long crtTime = 0;
long scrollInterval = 100;
// Initialize 16x2 LCD display:
// pin 1 - Reset (RS)
// pin 2 - Enable (E)
// pin 3 - D4
// pin 4 - D5
// pin 5 - D6
// pin 6 - D7
LiquidCrystal lcd(4, 2, 5, 18, 19, 21);
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.setCursor(0, 0); // Start from top left
lcd.print("Scrolling text");
delay(2000); // Pause for 2 seconds
}
void loop() {
// Get the current time in ms since the program started
crtTime = millis();
// Determine if the scroll interval has elapsed
if (crtTime - prevTime > scrollInterval) {
// Update the previous scroll time to the current time
prevTime = crtTime;
// Scroll the display one position to the right
lcd.scrollDisplayRight();
// Increment the psoition counter
i++;
// If the position counter is higher or equal to the
// number of characters on each row, the text is outside
// the visible area so reset it to the outside of the
// visible are in the other direction
if (i >= numCharacters) {
for (int j = 0; j < 2*i; j++) {
lcd.scrollDisplayLeft();
}
}
// Reset the position counter
i = -numCharacters;
}
delay(10); // this speeds up the simulation
}