// 80 char array for 4x20 LCD
// https://forum.arduino.cc/t/80-char-array-for-4x20-lcd/1325371/34


// Static data, update on change data, constantly updating data

#include <LiquidCrystal_I2C.h>
int lcdWidth = 20, lcdHeight = 4;
LiquidCrystal_I2C lcd(0x27, lcdWidth, lcdHeight);  // I2C address 0x27, 20 column and 4 rows
int xx, yy, cc, hello = 5, pots = 11, asciiLO = 161, asciiHI = 254, potA0old = -1, potA1old = -1;

void setup() {
  Serial.begin(115200);
  randomSeed(analogRead(A3));
  lcd.init();
  helloWorld(hello); // static data
  lcd.backlight();
  delay(500);
}

void loop() {
  showPots(); // update on change
  scramble(); // constantly update
}

void showPots() { // only changing data
  int potA0 = analogRead(A0); // read pot
  int potA1 = analogRead(A1);

  if (potA0old != potA0) { // if data changes
    potA0old = potA0; // store new value
    lcd.setCursor(pots, 1); // place cursor
    potPad(potA0); // zero pad pot value
    lcd.print(potA0); // print data to lcd
  }

  if (potA1old != potA1) {
    potA1old = potA1;
    lcd.setCursor(pots, 2);
    potPad(potA1);
    lcd.print(potA1);
  }
}

void potPad(int value) { // pad a value with zeroes
  for (int i = 1000; i > 9; i /= 10) { // thousands, hundreds, tens
    if (value < i) //
      lcd.print("0"); // pad zero
  }
}

void scramble() { // constantly changing data
  xx = random(lcdWidth); // random col
  yy = random(lcdHeight); // random row
  cc = random(asciiLO, asciiHI); // random ascii character
  if ((xx < hello - 1 || xx > pots + 4) || (yy < 1 || yy > 2)) { // data wormhole
    lcd.setCursor(xx, yy); // place cursor on lcd
    lcd.write(cc);
  }
}

void helloWorld(int x) { // static data
  lcd.setCursor(x, 1);
  lcd.print("Hello,");
  lcd.setCursor(x, 2);
  lcd.print("World!");
  delay(500);
}