Moved transitions to state_machine function

Changed ints to unsigned chars.
Fixed delayMillis function and replaced __delay_cycle calls.