// Sketch from: https://www.instructables.com/Arduino-LCD-Display-FlyIn-Text-Effect/
// Diagram from: https://wokwi.com/projects/294342288335700490
// Code adjusted for other pins to the display.
// Sketch adjusted for I2C display and millis() by Koepel.
//
// I start the position outside the screen.
// That was easier, because then it is never the same as
// the final position. Or maybe it is just bad coding.
//
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp lcd;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
unsigned long previousMillisFlying;
const unsigned long intervalFlying = 80;
bool enableFlying;
unsigned long previousMillisActivate;
const unsigned long intervalActivate = 15000;
// 0123456789012345
char text[16] = "Come Fly With Me";
int index; // index to a characer in the text
int count; // count for scrolling/flying
void setup()
{
// Serial.begin(115200);
int result = lcd.begin(LCD_COLS, LCD_ROWS);
if (result)
{
Serial.print("LCD initialization failed: ");
Serial.println(result);
hd44780::fatalError(result);
}
index = strlen(text) - 1; // start at last character
count = -1; // start outside screen on the left
enableFlying = true;
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillisActivate >= intervalActivate)
{
previousMillisActivate = currentMillis;
// Start a new fly sequence
lcd.setCursor(0,0);
// 0123456789012345
lcd.print(" ");
index = strlen(text) - 1;
count = -1;
enableFlying = true;
}
if(enableFlying)
{
if(currentMillis - previousMillisFlying >= intervalFlying)
{
previousMillisFlying = currentMillis;
// remove previous character
if(count >= 0)
{
lcd.setCursor(count,0);
lcd.print(' ');
}
// show the character in the next position
count++;
lcd.setCursor(count,0);
lcd.print(text[index]);
// check if the character is in its final position
if(count == index)
{
index--; // select character on the left
count = -1; // start outside screen
// Ready with all the text ?
// Then stop this timer.
// The variables are set properly when started again.
if(index < 0)
{
enableFlying = false; // stop this timer.
}
}
}
}
}