controlling a relay with mutiple function and logic