'Bang' and Timer within Codebox?

joelsquare's icon

Hi all,

I realize this is probably a very basic question, but I am extremely new at Codebox-ing (and coding in general) and still so unfamiliar with the terms that I don't know what to search for. If someone could help me out, I'd be hugely appreciative, even if all you do is tell me the terms to search for.

I want to create an action in codebox that does the following:

Upon receiving a "1" on the left inlet (only a 1 - all other numbers should have no effect) it begins a timer that continuously sends a "1" out the left outlet until the timer reaches the value that has been set in the right inlet, at which point, it sends a "0" out the left outlet and waits to be triggered again. The trick is that I need the left inlet to immediately react to receiving 1 - however brief - but nothing else. It's like I want it to be able to receive a 'bang', perform a function, and then, when that function is complete, be able to receive another bang to do the whole thing over again.

Essentially, I'm looking to use codebox to create a re-triggerable timed gate control that allows you to set duration. I'm sure there are ways to do this by patching operators, but I feel like codebox would be the cleanest way to achieve this. Maybe I'm wrong?

Anyway, I do realize this is likely some very basic stuff, but if someone could help me out by explaining, demonstrating, or even suggesting terms I should be researching, I'd be extremely appreciative.

Cheers,
J

Isabel Kaspriskie's icon

Perhaps something like this is what you're after?

I did it without codeboxing, but it's of course also possible in codebox (and you can see something like what that would look like by hitting the "C" button on the gen~ patcher's right toolbar).

Max Patch
Copy patch and select New From Clipboard in Max.

I find that the best use of codeboxing is when handling for-loops and highly iterative work, but often patching with operators is all you need.

joelsquare's icon

Wow - thank you so much for this! This is already almost exactly what I'm looking for and it won't be hard for me to tweak this to be precisely what I need. Huge gratitude.

You also pointed out something really useful to me - I've never considered looking at the code (using the C button) as a way to get familiar with the coding terms. I really want to learn code but find myself way too busy these days to focus on it, and so I set out to learn only what I need to learn to accomplish the patches I want to build. This goes a really long way in helping.

THANK YOU!

Isabel Kaspriskie's icon

Awesome! Glad it helped. :)