A great composer doesn't borrow, he steals
On of the motivations of opensource software is to share software solutions, hoping to save the time and energy of "reinventing the wheel". For this reason, I recommend whole-sale stealing of working patches from wherever you can find them. In this case, the PD Help system provides several solutions to the initial problem of builiding an event counter, that of the couter machanism itself. Here's one stolen from "2.control.examples.pd -> 06.more.counters.pd":
Perfect. Our event counter will do the same, count to 10 and then reset...assuming out piece has 10 events.
The center of the action is the [float] object, a simple place holder for a number (all numbers in PD are floats). [metro] is a metronome that sends a bang every time increment, in this case every 500ms (0.5 sec). The bangs from metro tell [float] to pass along the number it's holding, initally "0". On the first bang, that "0" goes on to the [+ 1], adding 0+1, and the new value is returned to [float] at it's left, "cold" inlet. The new value, "1", is thus waiting in [float] to be passes along at the next bang. 500ms later, [metro] bangs [float] once again. [float] passes the "1", which is again passed to [+1] and back into [float] as "2" (1+1), and so on. The only thing stopping [float] from counting forever is [select]. When [select] sees "10", it sends a bang out of its left-most outlet, banging a [stop ( message for [metro] and stopping the counter.
Counting in response to a click
We'll make several small changes to this mechanism, removing the metronome so that the counter only advances when the mouse it clicked, and using [select 10] to reset the [float] value to "0". Lately, I have taken to building mouse foot pedals for performers out of old one-button Mac mice, a process I will document here in the future. The key object is from the GEM library, called "MouseState" which checks whether the mouse is clicked, where it is on the screen, etc. In our case I just want the click state, up or down (0 or 1). In the patch below I use "select" to set the "polarity" of the mouse foot swtich so that it clicks when the foot is released/up. When select gets a "0" (mouse click up) it bangs our counter. Many players prefer this but it is easily reversed ([select 1 0] for those who don't.
I have added one other trick: the ability to filter out unwanted bangs. The object [oneshot] only passes along the first bang it receives and will not pass another until it gets the message [clear ( at it's left inlet. In this case, [clear ( is sent by [delay] one second after the initial bang. In other words, oneshot will not let through any click/bang that hapens less than a second after the previous. And, of course, any bang also resets the one second [delay] and the process starts over. This filters out extraneous or accidental bangs. You can make this window shorter of longer by changing the value of [delay]. See the final result in the attached patch.