fixing bugs on routine to arduino code