Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol

//master
#include<LiquidCrystal.h>//header for lcd
#include<Wire.h>//header for I2C communication
LiquidCrystal lcd(12,11,5,4,3,2);//lcd module pins
void setup()
{
lcd.begin(16,2);//enable the lcd
lcd.setCursor(3,0);//set the cursor(3 column,0 row)n lcd
lcd.print("MASTER");//print master in lcd
lcd.setCursor(1,1);//set the cursor in lcd column row manner
lcd.print("arduino 1");
delay(5000);//give 5 second delay
lcd.clear();//clear the lcd display
Wire.begin();//begin I2C communication at pin A4,A5
}
void loop()
{
Wire.requestFrom(2,1);//request one byte from slave
byte x= Wire.read();//save the byte in x
float value=analogRead(A0);//read analog value
byte Mastersend=map(value,0,1023,0,127);//convert the analog value to digital
Wire.beginTransmission(2);//start transmission to slave
Wire.write(Mastersend);//send one byte converted digital value
Wire.endTransmission();//stop transmitting
lcd.setCursor(0,0);//set the cursor in lcd
lcd.print("FLEX SENSOR VAL:");//print in lcd
lcd.setCursor(7,1);
lcd.print(x);//print the value received from slave
delay(5000);
}
//slave
#include<Wire.h>//libraray for i2c communication
#include<LiquidCrystal.h>//library for lcd
LiquidCrystal lcd(12,11,5,4,3,2);//define the lcd module pins
byte slavereceived=0;//initialize
void setup()
{
lcd.begin(16,2);//initialize lcd display
lcd.setCursor(3,0);//set cursor in lcd
lcd.print("SLAVE");//print slave in lcd
lcd.setCursor(1,1);//set cursor in lcd
lcd.print("arduino 2");//print in lcd
delay(5000);//delay 5 seconds
lcd.clear();//clear the lcd
Wire.begin(2);//begin i2c communication slave address 2
Wire.onReceive(receiveEvent);//function call when slave receives value from master
Wire.onRequest(requestEvent);//function call when master request value from slave
}
void loop(void)
{
lcd.setCursor(0,0);//set cursor in lcd
lcd.print("FORCE SEN VALUE:");
lcd.setCursor(7,1);
lcd.print(slavereceived);//print the value received in master
delay(5000);
lcd.clear();//clear lcd
}
void receiveEvent(int value)//when this function is call slave receive value from master
{
slavereceived=Wire.read();
}
void requestEvent()//when master want value from slave
{
int value=analogRead(A0);
byte slavesend=map(value,0,1023,0,127);
Wire.write(slavesend);
}