user19878: I'm doing a project that includes an Arduino Uno and custom hardware made by me. Everything is working perfectly independently, and I'm trying to implement a way for them (the Arduino and my hardware) to work together. In order to achieve that, I am using the Wire protocol (Arduino side) and I2C (custom hardware side). From now I can send data from my hardware to my Arduino in a master-slave relation, and I have no problem with that. Please take a look at my Arduino code for better understanding:
void setup(void) {
  Wire.begin(7);
  Wire.onReceive(receiveEvent);
}

void receiveEvent(int howMany) {
  int x = Wire.read(); // receive byte as an integer
  Serial.print(x);
  if (x==0) 
    state = 0;
}

So far so good. Now I want to send data from the Arduino to my custom hardware full duple, and for that I am adding this code:
void frame(void){
  Wire.write('x');
  Wire.write('\r');
}

void loop() {
  if (state == 0){
    frame();
    state = 1;
  }
}

In order to receive the 'x' char in my custom hardware I have switched the I2C protocol bit from Write to Read using I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, _address, I2C_WRITE); to I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, _address, I2C_READ); in my custom hardware code.
I don't know why, I am not receiving the data from the Arduino. Am I missing any configuration on the Arduino side?

user10394: I've not done events on an Arduino so this could be a bum steer, but shouldn't the state variable be volatile and protected with a semaphore?
You are accessing it in the main program and in your event/ISR.
Secondly, no offence intended, could you replace your hardware with a bit of tried and trusted hardware to eliminate the remote possibility that your hardware is at fault.

user11763: You use the onReceive() and onRequest() methods of the Wire library to implement an Arduino slave. In your case, something like this:
void setup(void){
  Wire.begin(18);
  Wire.onReceive(receiveEvent);  # register receive handler
  Wire.onRequest(requestEvent);  #register request handler
  Serial.begin(9600);
}

void receiveEvent(int howMany){
  int x = Wire.read();    // receive byte as an integer
  Serial.print(x);
  if (x == 0) 
    state = 0;
}

void requestEvent(){
  Wire.write("x\r");
  state = 1;
}

See Slave Receiver and Slave Sender examples for more details.

19878: <p>I'm doing a project that includes an Arduino Uno and custom hardware made by me. Everything is working perfectly independently, and I'm trying to implement a way for them (the Arduino and my hardware) to work together. In order to achieve that, I am using the Wire protocol (Arduino side) and I2C (custom hardware side). From now I can send data from <strong>my hardware</strong> to <strong>my Arduino</strong> in a <strong>master-slave</strong> relation, and I have no problem with that. Please take a look at my Arduino code for better understanding:</p>

<pre class="lang-c prettyprint-override"><code>void setup(void) {
  Wire.begin(7);
  Wire.onReceive(receiveEvent);
}

void receiveEvent(int howMany) {
  int x = Wire.read(); // receive byte as an integer
  Serial.print(x);
  if (x==0) 
    state = 0;
}
</code></pre>

<p>So far so good. Now I want to send data from the <strong>Arduino</strong> to my <strong>custom hardware</strong> full duple, and for that I am adding this code:</p>

<pre class="lang-c prettyprint-override"><code>void frame(void){
  Wire.write('x');
  Wire.write('\r');
}

void loop() {
  if (state == 0){
    frame();
    state = 1;
  }
}
</code></pre>

<p>In order to receive the 'x' char in my custom hardware I have switched the I2C protocol bit from Write to Read using <code>I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, _address, I2C_WRITE);</code> to <code>I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, _address, I2C_READ);</code> in my custom hardware code.</p>

<p>I don't know why, I am not receiving the data from the Arduino. Am I missing any configuration on the Arduino side?</p>
 10394: <p>I've not done events on an Arduino so this could be a bum steer, but shouldn't the state variable be volatile and protected with a semaphore?
You are accessing it in the main program and in your event/ISR.</p>

<p>Secondly, no offence intended, could you replace your hardware with a bit of tried and trusted hardware to eliminate the remote possibility that your hardware is at fault.</p>
 11763: <p>You use the <code>onReceive()</code> and <code>onRequest()</code> methods of the Wire library to implement an Arduino slave. In your case, something like this:</p>

<pre><code>void setup(void){
  Wire.begin(18);
  Wire.onReceive(receiveEvent);  # register receive handler
  Wire.onRequest(requestEvent);  #register request handler
  Serial.begin(9600);
}

void receiveEvent(int howMany){
  int x = Wire.read();    // receive byte as an integer
  Serial.print(x);
  if (x == 0) 
    state = 0;
}

void requestEvent(){
  Wire.write("x\r");
  state = 1;
}
</code></pre>

<p>See <a href="https://www.arduino.cc/en/Tutorial/MasterWriter" rel="nofollow noreferrer">Slave Receiver</a> and <a href="https://www.arduino.cc/en/Tutorial/MasterReader" rel="nofollow noreferrer">Slave Sender</a> examples for more details.</p>
