Refactor arduino code for better pin management

Also fix some pep8 errors and other cleanups