If you need to read buttons at the same time as you are doing other things, then you have "do several things at the same time"/multitasking problem. One way of solving it for buttons is doing the counting on the change-of-button-press-state with tricks as in: