When the program runs it can happen from time to time, that the LED driver did not send a proper response that it got the data through the I2C. When this happens the whole program stops. The watchdog then kills it and everything starts by the first frame.
Without putting the watchdog definition inside the for-statement the program does not start from the beginning and just keeps freezing. It freezes because the matrix will just keep the last frame of the animation until it gets a new one.
Here is the complete setup:
void setup() {
randomSeed(analogRead(1));
int b;
int c = random(1,10000);
for (b = 0; b < c; b++){
delay(1);
noInterrupts();
MCUSR &= ~_BV(WDRF);
WDTCSR = _BV(WDCE) | _BV(WDE);
WDTCSR = 0;
interrupts();
}
uint8_t i, j, p, byteCounter, byteCounterB;
uint8_t array13[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
uint8_t array36[36] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
power_all_disable();
power_twi_enable();
DIDR0 = 0x0F;
pinMode(3, INPUT);
I2c.setSpeed(1);
I2c.pullup(0);
I2c.timeOut(28); // Set timeout to 28 ms
I2c.begin();
pageSelect(0x0B);
I2c.write(I2C_ADDR1, 0, array13, 13);
for (p = 0; p < 2; p++) {
pageSelect(p);
I2c.write(I2C_ADDR1, 0, array36, 36);
I2c.write(I2C_ADDR1, 0x24, img, 144);
}
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
noInterrupts();
MCUSR &= ~_BV(WDRF);
WDTCSR = _BV(WDCE) | _BV(WDE);
WDTCSR = _BV(WDIE) | _BV(WDP0);
interrupts();
}