EDIT: solve! I had pin 4 and pin 5 of the digital I/O instead of the analog I/O. Stupid!!!
Hello,
I'm trying to follow the I2C example from this forum and from a couple of other places. A "writer" arduino sends a struct with two integers. A "reader" Arduino prints out the two ints it reads. Hardware wise, I have pin4, pin5, and ground connected between the two Arduinos. Pin4 to Pin 4 and Pin5 to Pin5, which are suppose to be the analog pins doing the I2C. I can verify that the "Writer" Arduino is sending values X: 1, 2, 3, 4...etc. But the Reader Arduino isn't seeing the data, and is not executing receiveEvent().
I'd appreciate any input on why this might not be working.
"Writer" Arduino, sending the values
// I2C communication between 2 Arduinos
// Analog joystick connected to analog pins A0 and A1
// Values from joystick are sent to the slave
#include <Wire.h>
const byte SLAVE_ADDRESS = 42;
int startx = 0;
int starty = 10;
void setup()
{
Serial.begin (9600);
Wire.begin ();
} // end of setup
void loop()
{
struct
{
int X;
int Y;
} XY;
startx = startx + 1;
starty = starty + 1;
XY.X = startx;
XY.Y = starty;
if (startx > 100)
{
startx = 0;
}
if (starty > 100)
{
starty = 0;
}
Wire.beginTransmission (SLAVE_ADDRESS);
Wire.write ((byte *) &XY, sizeof XY);
Wire.endTransmission ();
Serial.println (XY.X);
delay(4000);
}
"Reader" Arduino, reading the values from I2C
// I2C communication between 2 Arduinos
// Slave receives the integers X and Y from the master
// Values X and Y are printed to the serial monitor
#include <Wire.h>
const byte MY_ADDRESS = 42;
void setup()
{
Wire.begin (MY_ADDRESS);
Serial.begin (9600);
Wire.onReceive (receiveEvent);
Serial.println ("hi hi");
} // end of setup
volatile struct
{
int X;
int Y;
} XY;
volatile boolean haveData = false;
void loop()
{
if (haveData)
{
Serial.print ("Received X = ");
Serial.println (XY.X);
Serial.print ("Received Y = ");
Serial.println (XY.Y);
haveData = false;
} // end if haveData
} // end of loop
// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
{
if (howMany < sizeof XY)
return;
// read into structure
byte * p = (byte *) &XY;
for (byte i = 0; i < sizeof XY; i++)
*p++ = Wire.read ();
haveData = true;
Serial.println ("I see something!");
}
I got this from Nick Gammon's 4th post in this thread:
http://forum.arduino.cc/index.php?topic=126936.0