I2C Communication Master as sender and reader , Slave as reader and sender.

I'm new at using Arduino. And i got a task from my lecture to build this things

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include "TextCommand.h"
#include <Wire.h>
TextCommand cmd(&Serial, 100);
TextCommand i2c(&Wire, 100);

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void SerCommand() {
  Wire.beginTransmission(2);
  int n = cmd.available();
  if (n > 0) {
    if (cmd.isWrite() == 0) {
      if (cmd.ucAddress() == 0)
        switch (cmd.ioAddress()) {
          case 0:
            {
              Serial.println("cek koneksi ");
              Wire.println("!R0200");
              Wire.endTransmission();
               Wire.requestFrom(2, 8);    // request 6 bytes from slave device #8

   Wire.available(); { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.println("!R0000");         // print the character
  }
              break;
            }
        }}}}
void loop() {
  SerCommand();

  
        
  
}

and below is the slave program

#include <Wire.h>
#include <TextCommand.h>

TextCommand i2c(&Wire, 100);
TextCommand cmd(&Serial, 100);

void setup() {
  Wire.begin(2);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop() {
i2cCommand();
  
}


void i2cCommand()
{
  int n = i2c.available();
  if ( n > 0)
  {
    if (i2c.isWrite() == 0) {
    if (i2c.ucAddress() == 2) {
      switch (i2c.ioAddress()) {
        case 0:
    Serial.println("!R0000");
    Wire.write ("c");
    Serial.println ("!R0200");
    
      }
    }
    }
  }
}

 void receiveEvent(int howMany)
{
  i2cCommand();
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  i2cCommand(); // respond with message of 6 bytes
  // as expected by master
}

I need to sent back the data from the slave. but i dont know how to do it. is there anyone can help me with the case?

gajulan:
I need to sent back the data from the slave. but i dont know how to do it. is there anyone can help me with the case?

Who is your Master - UNO, NANO, MEGA?
Who is your Slave - UNO, NANO, MEGA?

What data (give exact example) do you want to send from Master to Slave?
What data (give exact example) do you want to receive from Slave?

im using uno for the master
and nano for the slave.

data[2] = (char*)cmd.data();
tim = atoi(data[2]);
Serial.print("!w0012 ");
Serial.println(tim);

data[0] = (char*) cmd.data();
LLD.analogWrite(atoi(data[0]));
Serial.print("!w0010 ");
Serial.println(data[0]);
Wire.println("!w0210 ");

data[1] = (char*)cmd.data();
ULD.analogWrite(atoi(data[1]));
Serial.print("!w0011 ");
Serial.println(data[1]);
Wire.println ("w0211 ");
break;

i want to send this kind of data to my master. but im confusing how do i need to write in
x x x = Wire.read()

i already send my master data to slave
this data is the kind of command like

i sent a serial command from my master to the slave
like !R0000 and then the slave respond it. But i dont get any idea how to send it back.

im stuck

!R0000

The last four characters are zero (0) or O.

its 0 not O

ive attach the master slave program. hope you can help me

I can't open your zip file.

You may carry out the following steps to send the command !R0000 to Slave.

1. Connect UNO and NANO using I2C Bus as per following diagram.
i2cCon.png

2. Upload the following sketches in UNO and NANO.
Master Codes:

#include<Wire.h>

void setup() 
{
  Serial.begin(9600);
  Wire.begin();

  Wire.beginTransmission(0x08); //slave address
  byte busStatus = Wire.endTransmission();
  if(busStatus != 0x00)
  {
    Serial.print("Communication error...!");
    while(1);
  }
  Serial.println("Slave found...!");
}

void loop() 
{
  Wire.beginTransmission(0x08); //slave address
  Wire.print("!R0000");
  Wire.endTransmission();

  delay(1000);  //test interval
}

Slave Codes:

#include<Wire.h>
volatile bool flag1 = false;
char myData[20] = "";

void setup()
{
  Serial.begin(9600);
  Wire.begin(0x08); //slave address
  Wire.onReceive(receiveEvent);
}

void loop()
{
  if (flag1 == true)
  {
    Serial.print(myData);
    char myData[20] = "";
    flag1 = false;
    Serial.println();
  }
}

void receiveEvent(int howMany)
{
  for (int i = 0; i < howMany; i++)
  {
    myData[i] = Wire.read();
  }
  flag1 = true;
}

3. Check that NNAO has received the message !R0000 from UNO.

Screenshot of Slave:

i2cCon.png