Using a button library (eg Toggle) and a state machine approach (here is a small introduction to the topic: Yet another Finite State Machine introduction) would make this pretty trivial to write esp. if your button is bouncing.
1 Like