The easiest way of doing this is with a switch statement
Also noticed that I do not take the reading straight away -> I wait for 100 positive readings in a row before I change state.Code:switch (state) { case 0: if (button_pressed) { count += (count < 100); if (count == 100) { count = 0; state = 1; } else if (count > 100) { count = 0; } } else { count = 0; } break; case 1: if (!button_pressed) { count += (count < 100); if (count == 100) { count = 0; state = 0; } } else { count = 0; } break; }