#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
Serial.begin(9600); // Initialize serial communication
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
Serial.println("Enter Text:");
lcd.clear(); // Clear the display
}
void loop() {
// Step 1: Take user input only once
static String userInput;
if (userInput.length() == 0) {
userInput = getUserInput();
}
// Step 2: Scroll text from left to right and vanish
scrollText(userInput);
// Step 3: Collide with walls two times
collideWithWalls(userInput);
// Step 4: Center text and blink three times
centerTextAndBlink(userInput);
// Repeat the steps by clearing the LCD and using the same input
lcd.clear();
delay(1000); // Wait for 2 seconds before starting again
}
String getUserInput() {
lcd.clear();
lcd.setCursor(0, 0);
while (!Serial.available()) {
delay(100); // Wait for user input
}
String input = Serial.readString();
return input;
}
void scrollText(String text) {
for (int i = 0; i < text.length() + 16; i++) {
lcd.clear();
lcd.setCursor(i, 0);
lcd.print(text);
delay(200);
}
}
void collideWithWalls(String text) {
int lcdWidth = 16; // Assuming LCD width is 16 characters
// Scroll text from left to right and collide
for (int j = 0; j < 2; j++) {
for (int i = 0; i <= lcdWidth - text.length(); i++) {
lcd.clear();
lcd.setCursor(i, 0);
lcd.print(text);
delay(200); // Adjust delay as needed for desired scrolling speed
}
// Scroll text from right to left and collide
for (int i = lcdWidth - text.length(); i >= 0; i--) {
lcd.clear();
lcd.setCursor(i, 0);
lcd.print(text);
delay(200); // Adjust delay as needed for desired scrolling speed
}
}
}
void centerTextAndBlink(String text) {
int lcdWidth = 16; // Assuming LCD width is 16 characters
int centerOffset = (lcdWidth - text.length()) / 2;
// Scroll text from left to center
for (int i = 0; i <= centerOffset; i++) {
lcd.clear();
lcd.setCursor(i, 0);
lcd.print(text);
delay(200); // Adjust delay as needed for desired scrolling speed
}
// Blink text three times
for (int j = 0; j < 4; j++) {
lcd.clear();
lcd.setCursor(centerOffset, 0);
lcd.print(text);
delay(400);
lcd.clear();
delay(400);
}
}