Hello, I have an arduino reading an I2C accelerometer, and a LEGO NXT reading the arduino over I2C. I've discovered that while using the code below, the NXT will only make a successful read if the serial.begin() line is commented out. Even then, only about 1/2 of the reads are successful. The NXT is requesting 1 byte of data every 500 ms.
If the serial.begin() line is not commented out the arduino program will hang after the NXT requests a byte. The last thing I see in the serial monitor is the "sent" confirmation at the end of the requestEvent() function.
What is the confliction with the serial, and I2C communication with a LEGO NXT. (btw, everything works perfectly if another arduino reads the first arduino instead of a NXT).
Here is my code:
#include "Wire.h"
/*
I2C hardware addresses for HiTechnic accelerometer
42H x upper 8bits
43H y upper 8bits
44H z upper 8bits
45H x lower 2bits
46H y lower 2bits
47H z lower 2bits
*/
byte byte_to_send;
char upper_byte;
char lower_byte;
void setup()
{
Wire.begin(4);
//Serial.begin(19200);
Wire.onRequest(requestEvent);
}
void loop()
{
Wire.beginTransmission(1);
Wire.send(0x42);
Wire.endTransmission();
Wire.requestFrom(1, 1);
while(Wire.available())
{
upper_byte = Wire.receive();
}
Wire.beginTransmission(1);
Wire.send(0x45);
Wire.endTransmission();
Wire.requestFrom(1, 1);
while(Wire.available())
{
lower_byte = Wire.receive();
}
byte_to_send = (lower_byte & 3) + (upper_byte << 2);
//Serial.println(byte_to_send,DEC);
delay(100);
}
void requestEvent() {
Wire.send(byte_to_send);
//Serial.println("sent");
}