Reorganised and commented Arduino sketch.