very simple program stops working after some loops

hi!

I have a very simple program structure, and it works beautifully at first, but after a few iterations of the main loop, it just stops responding, and I have no clue why.

Basically, I receive bursts of 18 bytes over the serial interface at regular intervals (currently 50ms). These are simply six RGB values one after another. they are parsed and then converted into I2C commands for my LED driver chip. Nothing fancy, right?
As I said, it does exactly what it’s supposed to, my LEDs react and everything’s fine at first, but at one point the LEDs just stall, although nothing has changed, and the bytes are still coming in. It’s not always the same amount of frames that get sent before it stops, but the time it takes to happen is approximately constant.
Sending the bursts furhter apart just delays the problem.

Is some kind of buffer filling? I’ve even tried flushing the serial buffer after each burst, with no effect. I’ll post the code here, maybe someone can catch the problem?

// SDA is A4 to pin 4
// SCL is A5 to pin 5
#include <Wire.h>
#define max6955 B1100000

byte i;

byte reds[6]  ={
  0,0,0,0,0,0};
byte greens[6]={
  0,0,0,0,0,0};
byte blues[6] ={
  0,0,0,0,0,0};

byte led01=0;
byte led23=0;
byte led45=0;

void setup()
{
  WireSetup();
  Serial.begin(57600);
  Serial.flush();
}

void loop()
{
  // Read bytes from serial and store them in the variables
  if (Serial.available() >=18) {
    for(i=0;i<=5;i++) {
      reds[i]  =Serial.read();
      greens[i]=Serial.read();
      blues[i] =Serial.read();
    }
    // Read bytes from variables and write them over I2C
    WriteColors();
//    Serial.flush();
  }
}

void WriteColors() {
  led01=0;
  led23=0;
  led45=0;

  // Turn on LED if corresponding value is over 128
  
  if(reds[0]>=128)
    led01|=B01000000 ;
  if(greens[0]>=128)
    led01|=B00100000 ;
  if(blues[0]>=128)
    led01|=B00010000 ;

  if(reds[1]>=128)
    led01|=B00000100 ;
  if(greens[1]>=128)
    led01|=B00000010 ;
  if(blues[1]>=128)
    led01|=B00000001 ;

  if(reds[2]>=128)
    led23|=B01000000 ;
  if(greens[2]>=128)
    led23|=B00100000 ;
  if(blues[2]>=128)
    led23|=B00010000 ;

  if(reds[3]>=128)
    led23|=B00000100 ;
  if(greens[3]>=128)
    led23|=B00000010 ;
  if(blues[3]>=128)
    led23|=B00000001 ;

  if(reds[4]>=128)
    led45|=B01000000 ;
  if(greens[4]>=128)
    led45|=B00100000 ;
  if(blues[4]>=128)
    led45|=B00010000 ;

  if(reds[5]>=128)
    led45|=B00000100 ;
  if(greens[5]>=128)
    led45|=B00000010 ;
  if(blues[5]>=128)
    led45|=B00000001 ;

  // Perform I2C Write
  Wire.beginTransmission(max6955);
  Wire.send(0x20); 
  Wire.send(led01); // leds 4 and 5 in register 0x20
  Wire.send(led45); // leds 4 and 5 in register 0x21
  Wire.endTransmission();   
  Wire.beginTransmission(max6955);
  Wire.send(0x28); 
  Wire.send(led23); // leds 2 and 3 in register 0x28
  Wire.endTransmission();   
}

void WireSetup() {
  Wire.begin();
  Wire.beginTransmission(max6955);
  Wire.send(0x01); // config register
  Wire.send(0x00); // no decode!
  Wire.send(0x0F); // global current to max
  Wire.send(0x01); // only use digits 0 and 1
  Wire.send(0x01); // global current control, shutdown disabled
  Wire.endTransmission(); 

  for(i=0x20;i<=0x4F;i++) {
    Wire.beginTransmission(max6955);
    Wire.send(i); // digit registers
    Wire.send(0x00); // clear all
    Wire.endTransmission();   
  }
}

Thanks, guys!

Two points:- 1) Do you have pull up resistors on your I2C lines? 2) Have you any supply decoupling on the I2C chips? http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Other than that use print statements to try and see where it hangs.

wow! I used 4K7 pullups on the I2C lines and a 100uF capacitor on the supply lines, and the problem hast been showing up less often and after a longer time, so it looks I'm on the right track :) Any ideas on how I might adjust these values to eliminate it completely? Thanks!