Is there an button tap vs tap & hold common function?

Hi,

I am making a nice little tap vs tap & hold function for my buttons.
This it occurred to me - is there already a library to handle this in Oopys and/or GEN?

So far, I have tap and hold working well.
Started working on the “tap” part and would prefer to borrow code here so I can keep on with the core of the project. :slight_smile:

Thanks for you help.
Brett

Simplest thing is to run a timer and grab the time when the button is released. If it’s below a certain amount, treat as a tap, otherwise as a hold.

The simplest timer in gen~ is [accum] aka [+=]. Send a param button to the 1st input of [+=] and it will count in samples while the button is held, and stop counting when the button is released. So now you want to do two things: a) measure the time when the button is released, and b) reset the time to zero when the button is pressed again. Both of these are edges: a) happens when button goes from 1 to 0, b) happens when button goes from 0 to 1. Send the param through [change] to detect those transitions. Change will output 1 when the input rises, -1 when the input falls, and 0 when the input stays the same. A button outputs a step function so button → change will output single-sample spike of 1 when button gets pressed, and single-sample spike of -1 when button is released. So, send output of [change] to [== 1] to 2nd inlet of the [+=] to reset the count when button is pressed, and output of [change] to [== -1] to detect when button is released. Send this spike to 2nd inlet of a [latch], send the [+=] to 1st inlet of latch, and you have what you need.

Hi @grrrwaaa,

Thanks for the info! Looking at your code idea, seems a little simpler that what I created. :slight_smile:

I suspect I’ll have to iterate on it, given what you noted above.
Here is what I ended up trying out… not sure it is great, but it does work. :slight_smile:


----------begin_max5_patcher----------
1551.3ocwY01aaaCD9yo.8+.g9PWBliJeUhrao.sceY.aXEXqXenYnP1h1Qs
xhBRT4kUz+66HkkeKJJNyNMFHNRGORd2Ce3cGo+5ye1QAiMWqqCPuB8QzQG8
UPxQdYNIG0I3nf4IWOIOo1qXvDy745BavnEMZ0Wa8M7ymdJpoVixSrSt.YMn
uXtBUkUmULCkTjhlljm6dVmNSiNuvo9wmRPm00vHzO5dqsGmrb7glzSLME9I
g1IsrRWCVQhMyT7oMTg0oRQy7rhbs0a1j0jZZrchwchyR89fY7mOksbpKcdB
XLepROw1hRLAOjvEwDVrhH4wTNaDhnjgRgDjpXJdjjw3iPTFMTwIRrjfUTIi
6zjwCwn+wO7e64Oy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X.7.JW
ohE.sfGeGAKhepBVLu1ZbEJWu+DCgb3KHAGxWKYI2UaTuLiX7SDVvXr8mQHh
GDETjdPg9XDz+eLhVk8WN112em2bbMrE1TaZplzMhKSAhVadS001rB+cpsgZ
7V0t6EhGvTx2sojd.mRxtOkj0VDpfym5ymdHLB2llc0HnGH+Fu6qt842jumF
wVqQKMB7AvH7k2e+FguxWGRbP76EKj2yTdP2ZQ2Iura89P.r61tY+QrOPd4x
wZWVKOPS4tElrELneOiY0ZYjGosMOHi.+XE.wkmcGBfzEes+zjIkkWpqpWzm
1YCpU3yFucJG09dVQ66s46CpzWl00ERaoAAIUPldKjluop8pSut6DTAyMfaW
zj044NG1M201axuUN5kWK8aZRyL+oMw1T+oeWWzrnnBvMmlzja2BdFOaZVd9
DSdqgdzplfxaVTtPPayi5juT6OhvtqeFNnNfTgL3nLzH+SvCBw57nEchz0Kt
fqbE36prCpkQ3eRRYBl6I718itZ1vDU6bfURLu8IPDbNIxF8CNxRt25o9pB6
DWVYJMUcq0PmUq5Qi0LqJIMS29y90x36V56PeOUqBTwuJ3gmNDdN.uYK64Jt
hu9Q25Sc4heAffz5xvYZ20YBp8ef6g8YV
-----------end_max5_patcher-----------

Thanks,
Brett

Try this:


----------begin_max5_patcher----------
1894.3oc0ZkziaaCE9rmeED5TB5TCQsY6Bjhza8PAZPu1VXHawwloxTBhzyR
CR+sWtIapERI6YFOdNjwQ7QI8deusOQxucyDuUEOhndfeB7mfIS91MSlHGRL
vD80S71k9357TpbZdj86Vgp9OuaUxvYxQKV80eLodr6JHLJ9eQBIvfo95g42
YwdVNhIePA5Qo3MhK8OLM0bXOUhTpkXFjzbuaAd2kWjx7.+sw6gjtSNOueoB
KljRRYJa8VLYyxJzZl5wDNW7J.AwPwOwIxK3J2gm1thLjohId52ktVNlgIfI
Gr.wXe+laD+41Qhd436QSoqKJQ8gfwd8BUPaHypTxlivQOFcvr4FF87Eh+Ft
vvne1FTJiUsG2isD41VD2mbtq1yXEDOigWlgok4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1W27
8a9e.cxDciC
-----------end_max5_patcher-----------

Here’s another variant; this one will give a single-sample spike for a short-tap, or will set a gate high for a long press.


----------begin_max5_patcher----------
1933.3oc0asriaaCEcsmuBBsJEcpgndXaUfjltqKJPC51jBCYKN1LUhRPjdl
IMn8au7kr0KRIO9wLdQFGSRIcuGdt26gTze+tINqxeFQc.+L3yfIS99cSlHa
RzvD82m3jE+75zXpbXNqyyxPDly8p9XnmYx1SyIa.EkH9vzcgSjcju5q+Dzq
pQxtr7crTDSd2b0sVDyVuES1rrDslorFXXzT26A9dghOVLS7WO2otf+5vcBS
ptQPQa+6c2I9y8mnmP2lWxL6Jvi1UBmeg8jT7inoz04En+qGKNreCFpaU0D6
aEHk05rJlrwYuwYdpwa9BoColmhL4Pdu.GJlwJ2g6wWBr6KhqSN1U6XrbhSs
lWlfoEoweSzsutccSY4IReegIDwJZnlc8fPwGvPW427tdSudm6oWsCc4lcIn
m3Fdm3tMHx+A9HCysWvWbn4YHfZZDjluAul9Em9BG6268L48T7FRbpy86+ei
.IBjgrvEdCL0V+FgJ0.fFAl37.NE8Hpjh47xCilSNKJp07jZWh.19ZdYMpon
ILQ0DbeSknGwUW+gAFWxcGF2W1UJcdmmmUE8HtMbNeIgGiUwIkSfZSRNUQhy
Pzh30pKNgVLkOAUcCbpkMvURQfdR9xLWY.PPXsDbb9vlz70+MJoVhR9bSAhf
Ih7r7rvwLsCru6DzCw6RYKeHmvn3+QZFPwTPO8+f1L6sSgiH8gesDKl50CYS
INImHLhFSGhlqdbhTcpn55NibDj3hdtXNijiKF5jxcxczUwkhYqUonZ7TdTP
ddZyt1econGX5tKvDRKTjkWXtyR7lsVt1U47Nyrcuk8PWtin5cImXvVRierI
ZyhSS0A0Mu8OGSvYwLDCqlB7b22IhDyczsz0k4ooM7WUOO1SOIbh9ZzS3D1V
4CpNYfObbQEIxY+rbBdChxZ1FKdCsYKT12Tfdsl1sRGHujgx3kIXsF.ODASY
bwBOQ0CrhnUG.NHtpdfc8LjMZ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