//LCD 16x2 5
//Menampilkan tulisan di LCD I2C
//Tulisan statis di baris pertama
//Tulisan bergeser ke kiri pada baris kedua
//Link Youtube : https://www.youtube.com/watch?v=CJQGS5D8kl0 
//Link Wokwi : https://wokwi.com/projects/335681800210743892

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

// Define 16x2 LCD
int screenWidth = 16;
int screenHeight = 2;

// To move ONLY one line needs to move one character at a time

// Define two line

String line1 = " BIMBEL LISTRIK "; // stationary " LCD ep3: "
String line2 = " Selamat Datang "; // scroll this line " This is a one line scrolling tutorial! "

int stringStart, stringEnd = 0;
int scrollCursor = screenWidth; 

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.begin(screenWidth, screenHeight);
}

void loop() {

  lcd.setCursor(0, 0); // Seting the cursor on first row 
  lcd.print(line1); // To print line1 message
  lcd.setCursor(scrollCursor, 1); // Seting the cursor on first row and (scrolling from left end to right)
  lcd.print(line2.substring(stringStart,stringEnd)); // To print line1 first character "T"

  delay(500);
  
  lcd.clear(); // clear message
  
  if(stringStart == 0 && scrollCursor > 0){
    scrollCursor--; // Moving cursor from 16 to 0
    stringEnd++; // Character T, H, I, S ...
                 // it will print out character from 0 to 15 the whole length of the screen
  }
  else if (stringStart == stringEnd){ // start all over again
    stringStart = stringEnd = 0;
    scrollCursor = screenWidth;
  } 
  else if (stringEnd == line1.length() && scrollCursor == 0) { // if reach to the end character
    stringStart++;
  } 
  else { // it will print out character from (1 to 16) to end character (this case it's !))
    stringStart++;
    stringEnd++;
  }
}