How do I fix it so it exits from the routine?
You'll need to get rid of the WHILE WEND loops waiting for state changes on RB0.