Arduino script stops after random period of time

This did not help. Could it be that I'm asking it to perform too much maths, should I simplify the code?