#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long timestamp;
int screenWidth = 16;
int stringStart,stringEndRepeat, stringEnd = 0;
int scrollCursor = screenWidth;
int scrollCursorRepeat = screenWidth;
unsigned long scroll_timestamp = millis();
unsigned long blink_timestamp = millis();
volatile boolean blink_indicator = true;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
scrollTextDisplay("Press The Start Button", 0, 5, 200);
BlinkText("PressStart", 3, 1, 500);
}
void scrollTextDisplay(String Your_Text_Here, int Top_Or_Bottom, int Text_Spacing, int Scroll_Text_Delay) {
String line2 = " " + Your_Text_Here + " ";
if ((millis() - scroll_timestamp) > Scroll_Text_Delay){
if (stringStart == 0 && scrollCursor > 0 && line2.length()<screenWidth){
scrollCursor--;
stringEnd++;
lcd.setCursor(scrollCursor, Top_Or_Bottom);
lcd.print(line2.substring(stringStart,stringEnd));
Serial.println(stringEnd);
}
if (stringEnd >= screenWidth && line2.length() < screenWidth){
stringStart++;
stringEnd++;
scrollCursor = 0;
lcd.setCursor(scrollCursor, Top_Or_Bottom);
lcd.print(line2.substring(stringStart,stringEnd));
Serial.println(line2.length());
Serial.println(stringEnd);
if (stringEnd == (15 + line2.length())){
stringStart = 0;
stringEnd = 0;
scrollCursor=screenWidth;
}
}
if(stringStart == 0 && scrollCursor > 0 && line2.length()>=screenWidth){
lcd.setCursor(scrollCursor, Top_Or_Bottom);
lcd.print(line2.substring(stringStart,stringEnd));
scrollCursor--;
stringEnd++;
}
if (stringEnd >= screenWidth && stringEnd < (line2.length() + Text_Spacing) && line2.length()>=screenWidth){
lcd.setCursor(scrollCursor, Top_Or_Bottom);
lcd.print(line2.substring(stringStart,stringEnd));
stringStart++;
stringEnd++;
scrollCursor = 0;
Serial.println(line2.length());
Serial.println(stringEnd);
}
if (stringEnd >= (line2.length()) && line2.length()>=screenWidth){
lcd.setCursor(scrollCursor, Top_Or_Bottom);
lcd.print(line2.substring(stringStart,stringEnd));
lcd.setCursor((scrollCursorRepeat),Top_Or_Bottom);
lcd.print(line2.substring(0, stringEndRepeat));
stringEndRepeat++;
scrollCursorRepeat--;
stringStart++;
stringEnd++;
}
if (stringEndRepeat >= screenWidth && line2.length()>=screenWidth){
stringStart = 0;
scrollCursor = 0;
stringEnd = screenWidth;
scrollCursorRepeat = screenWidth;
stringEndRepeat = 0;
}
scroll_timestamp = millis();
}
}
void BlinkText (String Your_Text_Here, int Position_01to16, int Top_Or_Bottom, int Blink_Delay){
if ((millis() - blink_timestamp) >= Blink_Delay){
if (blink_indicator == true){
lcd.setCursor(Position_01to16, Top_Or_Bottom);
lcd.print(Your_Text_Here);
Serial.println(blink_indicator);
}
if (blink_indicator == false){
lcd.setCursor(0, Top_Or_Bottom);
lcd.print(" ");
}
blink_indicator = !blink_indicator;
blink_timestamp = millis();
}
}