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.

Update: This is what I’m trying to do I had someone a while back makde a Samd21 zero board for me. There is no Serial monitoring. However there are i2c pins available i read somewhere that i can do a arduino to arduino over i2c and can output it to a second arduino. Problem is i need the serial monitoring. So I’m not sure how to transmit that serial monitoring stuff form the Samd21 board through the i2c to a second arduino and have that output that to a serial monitor.

Joseph

Problem is in two parts:
Are you really sure you have Serial.print(c), not Serial.println(c)?

And about the question marks, that's just because you read garbage after "there". You request 32 bytes from your slave. You only send 17. The master is leading in how many bytes it will queue, it WILL grab that many bytes even if the slave isn't sending it. Aka, you read garbage.

So I would swap things around. Let the sending party be the master and the receiving and the slave.

Something like:

Sender

const byte SlaveAddress = 5;

void setup(){
  Wire.begin();
}

void loop(){
  Wire.beginTransmission(SlaveAddress);
  Wire.write("Hello world");
  Wire.endTransmission();
  delay(2000);
}

Receiver/serial bridge

const byte SlaveAddress = 5;

void setup(){
  Serial.begin(115200);
  Wire.begin(SlaveAddress);
  Wire.onReceive(wireHandler);
}

void loop(){
  
}

void wireHandler(int n){
  while(Wire.available()){
    Serial.write(Wire.read());
  }
}

@OP

Upload and study the following sketches to observe how your 3 data items are being sent by I2C-Master and then they are being received and displayed by the I2C-Slave. (You are terribly missing the fundamental understandings of the I2C bus programming and operation.)

I2C-Master Sketch (UNO)

#include <Wire.h>

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

void loop()
{
  Wire.beginTransmission(5);   //slaveAddress
  Wire.write("1234567890");
  Wire.write('\n');             //data item seperator
  Wire.write("hi");
  Wire.write('\n');            
  Wire.write("there");
  Wire.write('\n');
  Wire.endTransmission();
  delay(1000);    //test interval
}

I2C-Slave Sketch (NANO)

#include <Wire.h>
bool flag1 = false;
char myItems[50] = "";

void setup()
{
  Serial.begin(9600);
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
}

void loop()
{
  if (flag1 == true)
  {
    Serial.print(myItems);
    Serial.println("===========================");
  }
  flag1 = false;
}


void receiveEvent(int howMany)
{
  for (int i = 0; i < howMany; i++) //howMany is equal to the number of data bytes received
  {
    myItems[i] = Wire.read();
  }
  flag1 = true;
}

Receiver/Slave Screenshot
sm146.png

sm146.png

Hello GolamMostafa and Septillion Thank you very much i saw where my mistakes are. And where i was missing some things i had 32 bytes of stuff and i took out somethings to have 16 r 17 bytes on there and that left that backwards Questions mark. I'm taking a very close look at what i have and fixing it then i will upload new update sketch. Thank you very much for the help and showing me what i did wrong.

Joseph