The Wire library uses interrupts and uses time. Even if you capture all the interrupts, the time by the display library in the loop() might cause a miss of reading the flag.
You can start by adding a counter in a interrupt, instead of just setting a flag.
The Arduino Uno has a 8-bit microcontroller, if you use a 8-bit value to count (use byte
), then the code is easier.
The variable that is used both in a interrupt and the loop() should be volatile
.
I noticed this:
if (endCycle == beginCycle or endCycle == beginAverageCycle) while(1);
No buzzer, no flashing lights, no message to Serial Monitor, no message on display ?
You could set a variable to a error and print that once in a while. I prefer to let the sketch run at all times.