#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

bool GoGoGo = false;
unsigned long previousMillis = 0;
const long interval = 1000;
int countdown = 0;

int seg[] =  {1, 2, 3, 4, 5, 6, 7, 8};

int digit[][8] = {
  {1, 0, 1, 0, 0, 1, 0, 0}, // 2
  {1, 0, 1, 0, 0, 1, 0, 0}, // 2
  {0, 1, 1, 1, 1, 1, 1, 1}, // .
  {1, 1, 1, 1, 1, 0, 0, 1}, // 1
  {1, 1, 1, 1, 1, 0, 0, 1}, // 1
  {0, 1, 1, 1, 1, 1, 1, 1}, // .
  {1, 0, 0, 1, 0, 0, 1, 0}, // 5
  {1, 0, 1, 0, 0, 1, 0, 0}, // 2
  {1, 1, 0, 0, 0, 0, 0, 0}, // 0
  {1, 0, 0, 1, 0, 0, 0, 0}  // 9
};


byte char1[] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00011
};

byte char2[] = {
  B00000,
  B01100,
  B10010,
  B10010,
  B10001,
  B01000,
  B11100,
  B00000
};
byte char3[] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B01000,
  B00100,
  B00011
};

byte char4[] = {
  B00100,
  B00011,
  B00100,
  B00011,
  B00100,
  B00011,
  B00010,
  B00001
};

byte char5[] = {
  B00000,
  B11000,
  B00000,
  B11000,
  B00000,
  B11000,
  B00001,
  B11110
};

byte char6[] = {
  B00001,
  B00001,
  B00001,
  B00001,
  B00001,
  B01111,
  B10000,
  B00000
};



void setup() {
  lcd.init();
  lcd.backlight();
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.createChar(1, char1);
  lcd.createChar(2, char2);
  lcd.createChar(3, char3);
  lcd.createChar(4, char4);
  lcd.createChar(5, char5);
  lcd.createChar(6, char6);
  Serial.println("Ujian : Damar Galih");
  for (int i = 1; i < 9; i++) {
    pinMode(i, OUTPUT);
  }
}

void show() {
  for (int j = 0; j < 10; j++) {
    if (countdown <= 10) {
      lcd.setCursor(4, 0);
      if (countdown < 10) {
        lcd.print("0");
      }
      lcd.print(countdown);
      countdown++;
    } 
    for (int k = 0; k < 8; k++) {
      digitalWrite(seg[k], digit[j][k]);
    }
    delay(500);
    for (int k = 0; k < 8; k++) {
      digitalWrite(seg[k], HIGH); 
    }
    delay(500);
  }
}

void loop() {
  if (Serial.available() > 0) {
    String input = Serial.readStringUntil('\n');
    if (input.equals("GoGoGo")) {
      GoGoGo = true;
      previousMillis = millis();
    }
  }

  if (GoGoGo) {
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;

      lcd.setCursor(0, 0);
      lcd.write((byte)1);
      lcd.setCursor(0, 1);
      lcd.write((byte)4);
      lcd.setCursor(1, 0);
      lcd.write((byte)2);
      lcd.setCursor(1, 1);
      lcd.write((byte)5);
      lcd.setCursor(2, 0);
      lcd.write((byte)3);
      lcd.setCursor(2, 1);
      lcd.write((byte)6);
      lcd.setCursor(4, 1);
      lcd.print("22.11.5209");
      show();
    }
  }
}