Added state machine and supporting logic for reading a pin
even more stash