Just on speculation, because it is so hard to definitely blame on that, have you tried disabling the reset, or making it less frequent and monitoring the results?
I spent some time studying your code and I can't see anything wrong (I don't pretend that I fully understand it, though... it would take more time).
Also run a side campaign to rule out silly stuff, like swapping hardware components?