first sorry if this is covered in another topic, i searched but couldnt find any that covered this exactly.
i am trying to send an int from one arduino to another. My problem is that my code only works for numbers 0 - 255. im sure i am missing something simple. i would appreciate some help.
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
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
pinMode(13, OUTPUT);
}
void loop()
{
Wire.requestFrom(2, 2); // request 6 bytes from slave device #2
digitalWrite(13, HIGH);
while(Wire.available()) // slave may send less than requested
{
int i = Wire.receive(); // receive a byte as character
Serial.print(i); // print the character
Serial.println();
digitalWrite(13, LOW);
}
delay(500);
}
Slave code:
// 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
#include <Wire.h>
int i;
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
i = 0;
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.send(i); // respond with message of an int (2) bytes
// as expected by master
i = i++;
}