I have a Nano (Master) sending data to Nano (Slave) using I2C. I'm using the Wire examples included with Wire library. I edited the master_writer to send at string. The problem is the string is cut off and an unknow integer is added to the end. What am I doing wrong. Thanks
Master Code:
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
}
byte x = 0;
void loop() {
Wire.beginTransmission(8); // transmit to device #8
Wire.write("81,0.00,0.00,70.14,54.41,48.97,29.86,0,1"); // sends five bytes
Wire.endTransmission(); // stop transmitting
delay(500);
}
Slave Code:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
Slave Serial Monitor:
16:04:42.827 -> 81,0.00,0.00,70.14,54.41,48.97,50
16:04:43.339 -> 81,0.00,0.00,70.14,54.41,48.97,50
16:04:43.809 -> 81,0.00,0.00,70.14,54.41,48.97,50
16:04:44.325 -> 81,0.00,0.00,70.14,54.41,48.97,50
16:04:44.825 -> 81,0.00,0.00,70.14,54.41,48.97,50
16:04:45.336 -> 81,0.00,0.00,70.14,54.41,48.97,50
16:04:45.849 -> 81,0.00,0.00,70.14,54.41,48.97,50