arduino to arduino i2c

Thank you for taking the time to read this. I am sending a 3 digit number from one Arduino to the other with i2c .
I am ok with this the problems comes when I try to do something with the number because  the number is sent one byte at a time I don't know how to put it together at the other end 
so lets say I send "458"  this would arrive as "4" "5" "8"  could someone please tell me how make this back in to "four hundred and fifty eight" 


[code// 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.
char t = 0;

#include <Wire.h>

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

void loop() {
  Wire.requestFrom(8, 3);    // request 3 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
   char c1 = Wire.read();
     char c2 = Wire.read();
   
   
    Serial.print(c);         // print the character
 Serial.print(c1);
 Serial.print(c2);
  Serial.println(); 
  
    
  } 
  

  delay(500);
}]

Something like this...

int result = 0;

while (Wire.available())
{
  result *= 10;
  result += (Wire.read() - 48);
}

Serial.println(result);

Nick Gammon has a great article where he covers pretty much everything about I²C. Take a look at the eighth message: "Send and receive any data type".

thanks martin but that dose not work it gives me a value of -30374

I have had a quick look at nick's post but unfortunately it is a bit advanced for my understanding Pieter but thanks

Please show how you incorporated Martin-X's suggestion into your code?

Are you certain that you are sending and receiving "458" as a 3 byte character string?
Can you post the slave code?

Martin-X:
Something like this...

int result = 0;

while (Wire.available())
{
  result *= 10;
  result += (Wire.read() - 48);
}

Serial.println(result);

Magic numbers are bad. Instead of the 48, use the single character '0'. That way it is much more obvious what it is doing and you don't have to remember what 48 means.

int result = 0;

while (Wire.available())
{
  result *= 10;
  result += (Wire.read() - '0');
}

Serial.println(result);

Please show how you incorporated Martin-X’s suggestion into your code?

// 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.

int result = 0;
#include <Wire.h>

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

void loop() {
  Wire.requestFrom(8, 3);    // request 6 bytes from slave device #8

  while (Wire.available()){
  result *= 10;
  result += (Wire.read() - 48);
}

Serial.println(result);

  delay(500);
}

the slave code is

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

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

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("458"); // respond with message of 3 bytes
  // as expected by master
}

thanks

thank you martin and everyone for your help
this is the code I have ended up with my not be prefect but it works

int count =0;
int result = 0;
#include <Wire.h>

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

void loop() {
  Wire.requestFrom(8, 3);    // request 6 bytes from slave device #8

  while (Wire.available()){         //waiting for value
  result *= 10;                     // stuf to make number work ???
  result += (Wire.read() - 48);

count ++;                           // wait untill i have 3 bytes
if (count == 3){
 Serial.println(result);            // check result
 
 
 count=0;                          // reset counter

if (result == 500){               // check it all works 

 Serial.println("you got 500");


}
result = 0;                           // flush wire 
}
 
 }


  delay(500);
}

my next problem is;

how do I mark this post as completed

You can edit the first post and change the title there to add [solved]

ho dear just when you think you have it sorted the problem I have is when send lets say “946” then all works as it should

but if I try to send data such as count then it displays different numbers hope some can help me with this

thanks

Master code

// 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.
int count =0;
int result = 0;
#include <Wire.h>

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

void loop() {
  Wire.requestFrom(8, 3);    // request 3 bytes from slave device #8

  while (Wire.available()){         //waiting for value
  result *= 10;                     // stuf to make number work ???
  result += (Wire.read() - 48);

count ++;                           // wait untill i have 3 bytes
if (count == 3){
 Serial.println (result);            // check result
 
 
 count=0;                          // reset counter

if (result == 500){               // check it all works 

 Serial.println("you got 500");


}
result = 0;                           // flush wire 
}
 
 }


  delay(500);
}

slave code

#include <Wire.h>     // for i2c comunication
int count = 1;        // to count pulses from flow meter




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


}

void loop() {

 
Serial.println(count);


}
  


void requestEvent() {

 
 Wire.write(count);
delay(500);
 // Wire.write("964");
count ++;
}

I would change your approach and send and receive the two bytes of an integer, rather than sending a character string and creating a numerical value from that string. Do some searching on “send integer with i2c”.

The slave will break the previously declared and valued integer count into a high and low byte so the slave request event will look like this

void requestEvent() 
{
    byte buf [2];
  
    buf [0] = count>> 8; //High byte of integer
    buf [1] = count & 0xFF; //low byte of integer
    Wire.write (buf, 2); //slave must send all bytes with only one Wire.write command
 
}

The master is going to request two bytes and reassemble them back into an integer with bit shifting. Again have the integer to receive declared.

Wire.requestFrom(slaveID, 2);
count =  Wire.read() << 8 | Wire.read(); //first byte read is high byte

If you are unfamiliar with the bit shifting, you could use the arduino functions highByte(), lowByte() and word() to break and reconstruct the integer. See https://www.arduino.cc/en/Reference/HighByte