very simple program stops working after some loops


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]  ={
byte greens[6]={
byte blues[6] ={

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

void setup()

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

void WriteColors() {

  // Turn on LED if corresponding value is over 128
    led01|=B01000000 ;
    led01|=B00100000 ;
    led01|=B00010000 ;

    led01|=B00000100 ;
    led01|=B00000010 ;
    led01|=B00000001 ;

    led23|=B01000000 ;
    led23|=B00100000 ;
    led23|=B00010000 ;

    led23|=B00000100 ;
    led23|=B00000010 ;
    led23|=B00000001 ;

    led45|=B01000000 ;
    led45|=B00100000 ;
    led45|=B00010000 ;

    led45|=B00000100 ;
    led45|=B00000010 ;
    led45|=B00000001 ;

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

void WireSetup() {
  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

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

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?

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!