I'm sending a string via serial from Unity to an Arduino Mega with a baudRate of 115200. That string is parsed into an uInt_8 array and send to other arduino via i2c in packages of 12 bytes. This works great but only for the first 10 bytes (0-9) so it must have something to do with two decimals (10, 11). The string for 24 bytes looks like this ,0,255,0,055,0,025,0,255,0,etc. The values are always between 0/1, and 0/255.
void loop() {
int serialIndex = 0;
if(Serial.available() > 0){
while (0 < Serial.available()) { // loop through all the received bytes
String bufferString;
uint8_t bufferInt;
bufferString = Serial.readStringUntil(',');
bufferInt = bufferString.toInt();
serialBuffer[serialIndex] = bufferInt; // put current index byte in array
serialIndex ++; // add index.
}
sendBytes();
}
delay(50);
}
void sendBytes(){
for(int i = 0; i<boards; i++){
// int i2cIndex = i*12;
// for(int j = 0; j <12; j++){
// i2cBuffer[j] = serialBuffer[j+i2cIndex];
// }
Wire.beginTransmission(i+1);
Wire.write(serialBuffer, 12);
Wire.endTransmission();
}
}
Thanks for the nudge in the right way George Profenza.
I've changed this:
if(Serial.available() > 0){
to this:
if(Serial.available() == 30){
I also lowered the send frequency in Unity but i'm implementing a handshake now between arduino en unity for better speed and efficiency.