I2C send diffrent response

Hi guys,

I have a problem with the I2C bus (slave) at my Arduino Uno. Actually I programmed to receive datas from my Raspi. Write datas to the Arduino works fine, also it works to send a response form the arduino to the PI. But now there is the problem. I can just send a spezific response package.

I want to read with my Arduino 5 diffrent analog values an 7 digital values.

So the process is like this: I want to read with the PI a value form the arduino. It's no very time the same order from the values. So I net to send an request of e.g. outside temperature.

But at the Arduino I only get the requestevent but no datas, by them I may analysed which value I should send.

I just may do it, if i will say befor I get an value to read I need to send an write command to the arduino and after that I read. But then I need 2 transfer to read an value. Thats not really with good performance.

My routines are here:
// I2C callback for sending data
// This is called from the I2C master!
void sendData()
{

Wire.write( slaveWrite, 4); // slaveWrite is an Byte array with 4 spaces

}

// I2C callback for received data
void receiveData(int byteCount)
{
pos = 0;
while (Wire.available())
{
// read I2C value
buf[pos] = Wire.read(); // buf is an byte array as well.
pos++;
}

}

I hope you understand my problem and may help me.

Thanks ahead

is there no one, who knows something about this or has an idea?

ArmFPC:
is there no one, who knows something about this or has an idea?

No one understands your question.

As you correctly observed, the I2C bus is half duplex. You either send or receive. In you circumstance, you want the Arduino to control 4 sensors. A,B,C and D. You want the rPI to command the Arduino to sample a specific sensor and return the reading.

But you don't want to do two steps: {the following code needs to be translate to rPI commands}

  • send sample command:
    Wire.beginTransmission(arduinoSlaveId);
    Wire.write('A'); // command to sample sensor 'A'
    Wire.endTransmssion();
  • read sample value:
    Wire.requestFrom(arduinoSlaveId,4); // request four bytes of sample data
    while(Wire.available()){
    buf*=Wire.read();*
  • i++;*
  • }[/li]*
  • [/list]*
  • Your question asks how to do these two steps in one step?*
  • The only command sequence you could omit is the first one, but that would require you to read all 4 sensors every time:*
    • read sample value:*
  • Wire.requestFrom(arduinoSlaveId,16); // request sixteen bytes of sample data (4x4)*
  • while(Wire.available()){*
    _ buf*=Wire.read(); // buffer needs to be 16bytes long*_
    - i++;*
    - }[/li]*
    - [/list]*
    - No one could give you an answer because there is no acceptable answer to your question.*
    - Chuck.*

Thank you Chuck.

Your second answer is the correct on for me. So I can't do it how I want to do.

Then I will do it with your first description, that's not really performance but there is no other way.