I2C arduino communication

I am trying to send an integer between two arduinos using the Master Reader-Slave Writer configuration using the Wire.h library, but the example available is meant for strings. Can anyone tell me how to use the same config to send an integer?

Code for Master Reader - Program for Arduino 1 



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


#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, 6);    // request 6 bytes from slave device #8

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

   delay(500);
}

[Get Code]


Code for Slave Sender - Program for Arduino 2 



// 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("hello "); // respond with message of 6 bytes
   // as expected by master
}

Convert your integer to a string.

but the example available is meant for strings.

No, it is meant for a collection of bytes. Hint: an integer is a collection of (2) bytes.

PaulS:
No, it is meant for a collection of bytes. Hint: an integer is a collection of (2) bytes.

So can I just send an integer from the Slave and read it from the master with the following?:
requestFrom(8, 2);
while (Wire.available()) { // slave may send less than requested
int c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}

So can I just send an integer from the Slave and read it from the master with the following?:

No!

That reads ONE of the bytes.

You need to send the two bytes in some order (highByte() then lowByte() or the reverse, and then reassemble the int from the bytes (using the stupidly named word() macro).

PaulS:
No!

That reads ONE of the bytes.

You need to send the two bytes in some order (highByte() then lowByte() or the reverse, and then reassemble the int from the bytes (using the stupidly named word() macro).

I don't understand how I would implement that.
So say the integer I am trying to send from the slave is 13,789 (the range of what I will be trying to send will be between 10,000 and 14,000), how would I implement that?

So say the integer I am trying to send from the slave is 13,789

On the slave:

int slaveValue = 13789;

byte b[2] = { highByte(slaveValue),
                     lowByte(slaveValue) };

void requestEvent()
{
   Wire.write(b, 2); // respond with an array of two bytes
}

Then, on the master:

  int slavesValue = 0;

  byte b[2];
  while(Wire.available() < 2) {;} // Wait until there are 2 bytes
  b[0] = Wire.read();
  b[1] = Wire.read(); 

  slavesValue = (unsigned int)b[0] << 8 + b[1];

Thanks! Ill try it out as soon as I can and get back to you.