#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Mohamed Djouad Redouani
// Riadh Younes Harbouche
// Chakib Benchadi

LiquidCrystal_I2C lcd(0x27, 16, 2);

int x = 0;
const int LED = 13;

void setup() {
  Wire.begin(9);
  Wire.onReceive(receiveEvent);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("team 3");
}

void loop() {
  delay(5); // wait for 5ms
  Wire.beginTransmission(9);
  Wire.write(x);
  Wire.endTransmission();
  x++;
  if (x > 5) {
    x = 0;
  }
  delay(500);
}

void receiveEvent(int bytes) {
  if (Wire.available()) {
    char value = Wire.read();
    lcd.setCursor(0, 1);
    lcd.print("          "); // clear previous value
    lcd.setCursor(0, 1);
    lcd.print(value);
    if (value == '0') {
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      delay(200);
    } else if (value == '3') {
      digitalWrite(LED, HIGH);
      delay(400);
      digitalWrite(LED, LOW);
      delay(400);
    }
  }
}