#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int c = 0;
int s = 0;
int m = 0;
int h = 0;

int t1 = 0;
int t2 = 0;
int t3 = 0;
int t4 = 0;

int prix = 1;
int total = 0;

String es = "  ";
String L = "=====================";
String LOG = "FORMATIC";
String TG = "TOTAL GENERAL :";
String T24 = "TOTAL 24 H :";
String DH = "DH";
String TIR = "\n";
String PRIX = "PRIX :";
String T1 = "TUBE A :";
String T2 = "TUBE B :";
String T3 = "TUBE C :";
String T4 = "TUBE D :";


int b1 = 4;
int b2 = 5;
int b3 = 6;
int b4 = 7;

int d1 = 9;
int d2 = 10;
int d3 = 11;
int d4 = 12;

int c1 = 0;
int c2 = 0;
int c3 = 0;
int c4 = 0;




int cunter = 0;
int  buzzer = 13;


void setup() {

  Serial.begin(9600);


  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("FORMATIC DX-370");

  lcd.setCursor(0, 1);
  lcd.print("LODING DATA ...");
  delay(1000);

  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("PRIX:");

  lcd.setCursor(7, 0);
  lcd.print("TOTAL:");

  lcd.setCursor(0, 1);
  lcd.print("[  ][  ][  ][  ]");

  lcd.setCursor(13, 0);
  lcd.print(total);

  lcd.setCursor(5, 0);
  lcd.print(prix);

  lcd.setCursor(1, 1);
  lcd.print(t1);

  lcd.setCursor(5, 1);
  lcd.print(t2);

  lcd.setCursor(9, 1);
  lcd.print(t3);

  lcd.setCursor(13, 1);
  lcd.print(t4);
}

void loop() {

  Serial. println(s);



  c++;
  if (c == 10) {
    c = 0;
    s = s + 1;
  }
  if (s == 60) {
    s = 0;
    m = m + 1;
  }
  if (m == 60) {
    m = 0;
    h = h + 1;
  }
  if (h == 24) {
    h = 0;
  }
  if (s == 55)
  {
    Serial. println(LOG + TIR + T1 + t1);
    delay(5);
    //Serial. println(s);

  }



  digitalWrite(buzzer, 0);
  pinMode(buzzer, OUTPUT);

  pinMode(b1, INPUT_PULLUP);
  pinMode(b2, INPUT_PULLUP);
  pinMode(b3, INPUT_PULLUP);
  pinMode(b4, INPUT_PULLUP);

  pinMode(d1, INPUT_PULLUP);
  pinMode(d2, INPUT_PULLUP);
  pinMode(d3, INPUT_PULLUP);
  pinMode(d4, INPUT_PULLUP);

  b1 = digitalRead(4);
  b2 = digitalRead(5);
  b3 = digitalRead(6);
  b4 = digitalRead(7);

  d1 = digitalRead (9);
  d2 = digitalRead (10);
  d3 = digitalRead (11);
  d4 = digitalRead (12);


  ////////////// b1 PRIX ++ ////////////


  if (b1 == 0) {



    delay (100);
    digitalWrite(buzzer, 1);
    delay(50);
    digitalWrite(buzzer, 0);
    prix = prix + 1;
    c1 = 0;
    c2 = 0;
    c3 = 0;
    c4 = 0;


    lcd.setCursor(5, 0);
    lcd.print("  ");

    lcd.setCursor(5, 0);
    lcd.print(prix);
  }

  ////////////// b2 PRIX -- ////////////

  if (b2 == 0) {



    delay (100);
    digitalWrite(buzzer, 1);
    delay(50);
    digitalWrite(buzzer, 0);

    prix = prix - 1;
    c1 = 0;
    c2 = 0;
    c3 = 0;
    c4 = 0;



    lcd.setCursor(5, 0);
    lcd.print("  ");

    lcd.setCursor(5, 0);
    lcd.print(prix);
  }

  ///////////// STATE PRIX /////////////

  if (prix == 11) {

    prix = 1;

    c1 = 0;
    c2 = 0;
    c3 = 0;
    c4 = 0;

    lcd.setCursor(5, 0);
    lcd.print("  ");

    lcd.setCursor(5, 0);
    lcd.print(prix);
  }

  if (prix == 0) {

    prix = 1;

    lcd.setCursor(5, 0);
    lcd.print("  ");

    lcd.setCursor(5, 0);
    lcd.print(prix);
  }

  ////////// b 3 RESIT TOTAL ///////////

  if (b3 == 0) {


    delay (100);
    digitalWrite(buzzer, 1);
    delay(50);
    digitalWrite(buzzer, 0);

    total = 0;

    lcd.setCursor(13, 0);
    lcd.print("   ");

    lcd.setCursor(13, 0);
    lcd.print(total);
  }

  /////////// b 4 RESIT TUBE ///////////

  if (b4 == 0) {

    t1 = 0;
    t2 = 0;
    t3 = 0;
    t4 = 0;



    delay (100);
    digitalWrite(buzzer, 1);
    delay(50);
    digitalWrite(buzzer, 0);

    lcd.setCursor(1, 1);
    lcd.print("  ");

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print("  ");

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(" ");

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print("  ");

    lcd.setCursor(13, 1);
    lcd.print(t4);
  }

  if (d1 == 0) {

    c1 = c1 + 1;
    t1 = t1 + 3;
    cunter = cunter + 1;

    delay (100);
    digitalWrite(buzzer, 1);
    delay(100);
    digitalWrite(buzzer, 0);

    lcd.clear();

    lcd.setCursor(4, 0);
    lcd.print("1 DIRHAM");

    lcd.setCursor(4, 1);
    lcd.print(cunter);

    delay(500);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);
  }


  if (d2 == 0) {


    c2 = c2 + 2;
    t2 = t2 + 5;
    cunter = cunter + 2;

    delay (100);
    digitalWrite(buzzer, 1);
    delay(50);
    digitalWrite(buzzer, 0);

    lcd.clear();

    lcd.setCursor(4, 0);
    lcd.print("2 DIRHAM");

    lcd.setCursor(4, 1);
    lcd.print(cunter);

    delay(500);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);
  }

  if (d3 == 0) {


    c3 = c3 + 5;
    cunter = cunter + 5;

    delay (100);
    digitalWrite(buzzer, 1);
    delay(50);
    digitalWrite(buzzer, 0);



    lcd.clear();

    lcd.setCursor(4, 0);
    lcd.print("5 DIRHAM");

    lcd.setCursor(4, 1);
    lcd.print(cunter);



    delay(500);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);
  }

  if (d4 == 0) {


    c4 = c4 + 10;
    t3 = t3 + 5;
    cunter = cunter + 10;

    delay (100);
    digitalWrite(buzzer, 1);
    delay(50);
    digitalWrite(buzzer, 0);

    lcd.clear();

    lcd.setCursor(4, 0);
    lcd.print("10 DIRHAM");

    lcd.setCursor(4, 1);
    lcd.print(cunter);



    delay(500);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);


  }
  if (prix == 1 && c1 + c2 + c3 + c4 == 1)

  { cunter = 0;
    c1 = 0;
    c2 = 0;
    c3 = 0;
    c4 = 0;

    lcd.setCursor(0, 0);
    lcd.print("MONTANT VALIDER");

    delay(500);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);
  }




  if (prix == 1 && c1 + c2 + c3 + c4 == 2)


  { cunter = 0;
    c1 = 0;
    c2 = 0;
    c3 = 0;
    c4 = 0;
    lcd.setCursor(2, 0);
    lcd.print("CHANGE 1 DH");
    delay(500);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);






  }
  if (prix == 1 && c1 + c2 + c3 + c4 == 5)


  { cunter = 0;
    c1 = 0;
    c2 = 0;
    c3 = 0;
    c4 = 0;

    lcd.setCursor(2, 0);
    lcd.print("CHANGE 4 DH");

    delay(500);


    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);




  }
  if (prix == 1 && c1 + c2 + c3 + c4 == 10)
  {
    cunter = 0;
    c1 = 0;
    c2 = 0;
    c3 = 0;
    c4 = 0;

    lcd.setCursor(2, 0);
    lcd.print("CHANGE 9 DH");
    delay(500);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("PRIX:");

    lcd.setCursor(7, 0);
    lcd.print("TOTAL:");

    lcd.setCursor(0, 1);
    lcd.print("[  ][  ][  ][  ]");

    lcd.setCursor(13, 0);
    lcd.print(total);

    lcd.setCursor(5, 0);
    lcd.print(prix);

    lcd.setCursor(1, 1);
    lcd.print(t1);

    lcd.setCursor(5, 1);
    lcd.print(t2);

    lcd.setCursor(9, 1);
    lcd.print(t3);

    lcd.setCursor(13, 1);
    lcd.print(t4);

  }






}