2 dj hero decks and a crossfader - Buffer Overflow? i2c or serial?

I’ve got issues but not sure even which section of the forum to put them in.

My sketch works but after a while it hangs, stops sending data and I have to close and open the serial port to get it to send again.

I am reading the data in Max/Msp but it hangs the serial monitor in Arduino so I am assuming/guessing the problem is with the Arduino.

I have 2 dj hero decks connected via i2c and a crossfader which sends serial data to pin 0.

It seems to hang when the decks are spun really fast but I could be imagining it.

Does closing and reopening the port restart the sketch (ie the same as hitting reset on the Arduino? I didn’t think it did).

I’m not sure whether my problem is i2c or Serial port related.

Here is my sketch, its pretty simple, read the data from both decks and the crossfader, write the data and a few id bytes to the serial port.

If anyone can shed any light on this, or even help me track down the problem, you be my friend long time! Thanks

#include <Wire.h>

byte val = 0;
byte lturn=0;
byte rturn=0;
byte buf=0;
int cfade=0;
 int add=0;
 int avail=0;

void setup()
{
  Wire.begin(); // join i2c bus
  Serial.begin(38400);
}

void loop()
{
  Wire.beginTransmission(14); // transmit to device #44 (0x2c)
                              // device address is specified in datasheet
  Wire.write(val);             // sends value byte  
  Wire.endTransmission();     // stop transmitting
  Wire.requestFrom(14, 23);

 if (Wire.available()== 23)    // slave may send less than requested
  { 
     for (int i = 0; i < 20; i++) { 
       Wire.read();
    
  } 
   rturn=Wire.read();
    
     Wire.read();         // read the last 2 bytes
     Wire.read();         
  }
  
   Wire.beginTransmission(13); // transmit to device #44 (0x2c)
                              // device address is specified in datasheet
  Wire.write(val);             // sends value byte  
  Wire.endTransmission();     // stop transmitting
  Wire.requestFrom(13, 23);

 if (Wire.available()== 23)    // slave may send less than requested
  { 
     for (int i = 0; i < 20; i++) { 
       Wire.read();
    
  } 
  lturn=Wire.read();
     Wire.read();         // read the last 2 bytes
     Wire.read();         
  }
  
    
      
          
  
 
  Serial.write(rturn);
  Serial.write(200);
  Serial.write(lturn);
  Serial.write(70);
  Serial.write(cfade);
  Serial.write(143);
 
  delay(25);
}


void serialEvent(){
  
 
avail=Serial.available();
   for (int i = 0; i < avail; i++) { 
    buf = Serial.read();
   } 
    if (buf==255) {
    add=128; }
    else {
      
   cfade=buf % 128;
   cfade=cfade + add;
   //Serial.println(cfade);
   add=0;
    }
   
    
  }

Yes re starting the monitor does restart the arduino.

As i dont have the decks to sample a signal from i am not really sure if they could cause it or not but id say they could possibly output data too fast again not sure but it may be worth looking in to.

Try running the sketch with individual parts left out (ex. with out serial then with out i2c and see how the arduino handles them by there self.

I have tried it without the crossfader and it is the same
The wire library requests a certain number of bytes so hard to see how it could be sending too much data.
Looking at my code though I am wondering if I am leaving a byte in the buffer every time. I discard the last 2 bytes but maybe I should be discarding the last three.
I have to retain the serial.write or I won’t know if its crashed or not!
So I’m either overloading the serial or wire buffer or something else!

Some nice audio stff is wicked high performance and intended to work with other high performance devices. like I said I dont have one and i havent looked it up but that came from the you think it happens when its spun fast.

And maybe try serial printing the buffer value after its used and the 2 erased and see if theres any thing in there.

o i see now those are just game pads im sorry i assumed they were audio devices.

It occurs to me that if there were ever less than 23 bytes in the buffer eg 22 then the buffer is not read but another 23 bytes are requested.
Then there would more than 23 bytes and the buffer would still not ne cleared.
In fact it would never be cleared and would hang.
Something to try tomorrow, feel I might be onto something.
Perhaps fast spinning results in a lostbyte or two.
There is no contingency for lost data or for there being any other number of bytes available other than 23.
Very bad coding. Hopefully fixing it will be the answer