I did notice that replacing the debugging code with
Serial.println ("");
left it working, but actually made it work more poorly.
Basically, then, it's almost as if it's reading the situation too quickly? Perhaps I will go back to the less elegant code and see if it solves it.