Modularize the code

Modularize the code, separating hardware drivers from main state machine. The code has been tested via the web interface after every commit. The test method has been tested too.