Sorry, posting 5500+ lines of code would have been silly when this was more out of interest than anything else. Someone was able to come up with the solution without the code. I am more than happy to jump in and help others and usually the code is necessary but on this occasion it would have been ott.
I'm certain that it would have been possible to demonstrate the problem in considerably less than 5500 lines of code. Just the act of doing so would probably have been enough for you to spot the problem on your own. In any case, posting a minimal sketch that actually demonstrated the problem would have avoided relying on some inspired guesswork by majenko and would have avoided everyone else looking at this thread from wasting time trying to guess what you were doing. If you had posted a short sketch demonstrating the problem, it would have been blindingly obvious what was causing it.