I2C read and write multiple things?

Hello I’m having a hard time writing things on arduino to arduino over i2c read and write.

What I’m trying to do is put multiple things in transmitting and display them in the receiving end,. I will attach the sketch below. I learn this from a YouTube guy by the name of HumanHardDrive.

Here is the transmitting code.

#include <Wire.h>

void setup()
{
  Wire.begin(5);
  Wire.onRequest(requestEvent);
}

void loop()
{
  delay(100);
}

void requestEvent()
{
  Wire.write("1234567890");
   Wire.write("hi");
    Wire.write("there");
}

And here is the receiving sketch

#include <Wire.h>

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  
  Wire.requestFrom(5,32);
  
  while(Wire.available())
  {
    char c = Wire.read();
    Serial.print(c);
  
  }
}

void loop()
{
}

the output is

1
2
3
4
5
6
7
8
9
0
h
i
t
h
e
r
e
⸮
⸮
⸮
⸮
⸮
⸮
⸮
⸮
⸮
⸮
⸮
⸮
⸮
⸮

I can not get it to display line one at a time it looks like this. If i only do 10 bytes it will only display the 1234567890 correctly on on line. Also in this I’m getting a lot of backwards Question marks. Can someone please help me to fix this I’m so lost.

Joseph

You ask the slave for 32 bytes. You slave only writes 17 bytes, so of course you are going to get garbage when you read bytes 18 to 32.

The slave just jams bytes out. It is up to you to recognize how many bytes make uo a packet.

I can’t help but think that you are misusing I2C.

Anytime when you are sending and receiving data, both sides should agree on how much data, or at least a packet header that contains how much data to receive if you need dynamic payloads.

Insure both the sender and receiver understand how much data to expect to be sent, and recieved.

Hello somehow my Post got double and i don't know why or how can we please delete this one and use the other one?

Joseph

josephchrzempiec:
Hello somehow my Post got double and i don't know why or how can we please delete this one and use the other one?

Joseph

You should use the Report to Moderator link, and ask that the threads be merged.