// R Y B
int B3 = 13; // B3
int Y3 = 23; // Y3
int Y1 = 32; // Y1
int B2 = 33; // B2

int Y2 = 25; // Y2
int R1 = 26; // R1
int Bl = 27; // B1
int R2 = 14; // R2
int R3 = 12; // R3
const int LDR_PIN = 34;
const int ledPin =  18;
const int LDR_PIN2 = 39;
int b, r , y, x, ym, st, st2, buzzer = 19;
#include <LiquidCrystal_I2C.h>
int lcdColumns = 16;
int lcdRows = 2, f1, f2, f3, f4, f5, f6;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
byte symbol[8] = {
  B00000,
  B00100,
  B00100,
  B00100,
  B11111,
  B01110,
  B00100,
  B00000
};
void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  lcd.createChar(1, symbol);
  lcd.clear();
  lcd.setCursor(0, 0); // set the cursor to column 0, line 2
  lcd.print("Welcome to Cable");
  lcd.setCursor(0, 1); // set the cursor to column 0, line 2
  lcd.print("Fault Detection");
  delay(2000);
  lcd.clear();
  pinMode(R1, INPUT_PULLUP);
  pinMode(R2, INPUT_PULLUP);
  pinMode(R3, INPUT_PULLUP);
  pinMode(Bl, INPUT_PULLUP);

  pinMode(B2, INPUT_PULLUP);
  pinMode(B3, INPUT_PULLUP);
  pinMode(Y1, INPUT_PULLUP);
  pinMode(Y2, INPUT_PULLUP);
  pinMode(Y3, INPUT_PULLUP);
  analogReadResolution(10);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  if (digitalRead (R1) == LOW) {
    r = 1;
  }
  else if (digitalRead (R2) == LOW) {
    r = 2;
  }
  else if (digitalRead (R3) == LOW) {
    r = 3;
  }
  else {
    r = 0;
  }

  if (digitalRead (Y1) == LOW) {
    y = 1;
  }
  else if (digitalRead (Y2) == LOW) {
    y = 2;
  }
  else if (digitalRead (Y3) == LOW) {
    y = 3;
  }
  else {
    y = 0;
  }

  if (digitalRead (Bl) == LOW) {
    b = 1;
  }
  else if (digitalRead (B2) == LOW) {
    b = 2;
  }
  else if (digitalRead (B3) == LOW) {
    b = 3;
  }
  else {
    b = 0;
  }

  //  Serial.print(r);
  //  Serial.print("   ");
  //  Serial.print(y);
  //  Serial.print("   ");
  //  Serial.println(b);
  int LDR_Reading = analogRead(LDR_PIN);
  int LDR_Reading2 = analogRead(LDR_PIN2);
  //Serial.println(LDR_Reading);
  if (LDR_Reading < 140) {
    x++;
    ym = 0;
    if (x > 6) {
      digitalWrite(ledPin, HIGH);
    }
  }
  else {
    ym++;
    if (ym > 6) {
      digitalWrite(ledPin, LOW);
    }
    x = 0;
  }
  if (LDR_Reading2 > 0) {
    if (st == 0) {
      lcd.clear();
    }
    lcd.setCursor(0, 0);
    lcd.print("Battery Charging");
    delay(2000);
    st = 1;
    st2 = 0;
  }
  else {
    if (st2 == 0) {
      lcd.clear();
    }
    st2 = 1;
    st = 0;
    lcd.setCursor(1, 0);
    lcd.print("R");
    lcd.write(1);

    lcd.setCursor(7, 0);
    lcd.print("Y");
    lcd.write(1);

    lcd.setCursor(13, 0);
    lcd.print("B");
    lcd.write(1);
    String dt;
    dt = "R= ";
    if (r > 0) {
      lcd.setCursor(0, 1);
      lcd.print(r);
      lcd.print("KM");
      dt += r;
      dt += "KM";
      if (f1 == 0) {
        f1 = 1;
        f2 = 0;
        lcd.clear();
      }
    }
    else {
      if (f2 == 0) {
        f2 = 1;
        f1 = 0;
        lcd.clear();
      }
      lcd.setCursor(1, 1);
      lcd.print("NF");
      dt += "NF";
    }

    dt += "    Y= ";
    if (y > 0) {
      lcd.setCursor(6, 1);
      lcd.print(y);
      lcd.print("KM");

      dt += y;
      dt += "KM";
      if (f3 == 0) {
        f3 = 1;
        f4 = 0;
        lcd.clear();
      }
    }
    else {
      lcd.setCursor(7, 1);
      lcd.print("NF");
      dt += "NF";
      if (f4 == 0) {
        f4 = 1;
        f3 = 0;
        lcd.clear();
      }
    }

    dt += "    B= ";
    if (b > 0) {
      lcd.setCursor(13, 1);
      lcd.print(b);
      lcd.print("KM");

      dt += b;
      dt += "KM";
      if (f5 == 0) {
        f5 = 1;
        f6 = 0;
        lcd.clear();
      }
    }
    else {
      if (f6 == 0) {
        f6 = 1;
        f5 = 0;
        lcd.clear();
      }
      lcd.setCursor(13, 1);
      lcd.print("NF");
      dt += "NF";
    }
    if (r > 0 || b > 0 || y > 0) {
      digitalWrite(buzzer, HIGH);
      delay(500);
      digitalWrite(buzzer, LOW);
      delay(500);
    }
    else {
      digitalWrite(buzzer, LOW);
    }
  }
}