#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD: Address 0x27, 16 columns, 2 rows
const int DISPLAY_WIDTH = 16; // LCD width
unsigned long previousMillis = 0; // Stores the previous time
unsigned long interval = 600; // Scrolling interval in milliseconds
String inputMessage = ""; // Stores the message to be displayed
int scrollPosition = 0; // Current scrolling position
void setup()
{
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Serial.begin(9600); // Initialize serial communication
Serial.println("Start");
}
void loop()
{
// Check for serial input
if (Serial.available() > 0)
{
// Read the input message
inputMessage = Serial.readStringUntil('\n');
// Reset the scrolling position
scrollPosition = 0;
// Reset the scrolling timer
previousMillis = millis();
}
// Check if it's time to scroll
if (millis() - previousMillis >= interval)
{
// Clear the LCD
lcd.clear();
// Print the input message without scrolling if it's 16 characters or less
if (inputMessage.length() <= DISPLAY_WIDTH)
{
lcd.setCursor(0, 0);
lcd.print(inputMessage);
}
else
{
// Print the scrolling part of the message
lcd.setCursor(0, 0);
lcd.print(inputMessage.substring(scrollPosition, min(scrollPosition + DISPLAY_WIDTH, inputMessage.length())));
// Increment the scrolling position
scrollPosition++;
// Wrap around if needed
if (scrollPosition >= inputMessage.length())
{
scrollPosition = 0;
}
}
// Update the scrolling timer
previousMillis = millis();
}
}