Reputation: 113
I've confused my myself a lot and need your help.
I'm trying to make a little function in Pure Data which says when the toggle
is on it sends a 1
but when it isn't on it allows the computed value through.
So the program works a value 1
or 2
and when the toggle
isn't on the computed value goes through, but when the togggle
is on it sends 1
through.
I would show my code but its Pd so I can't copy and paste it. My attempt so far is making use of spigots
but that isn't working its still sending 1
through no matter what the toggle
is doing, and I know for a fact the computed value changes.
In normal code I would do something like:
value = computedVal
if (toggle == 1) then return 1;
else return value;
Upvotes: 3
Views: 13418
Reputation: 31374
Here's a solution using a demultiplexer idiom:
It basically prefixes the incoming message with a label (0
or 1
), and then routes the data accordingly.
In general, you should learn to think in data flow, rather than control flow: how do you get the data to that part of the patch where it is needed.
Trying to mimick control flow (if ... then ...
, or worse while ... do ...
) will only melt your brains.
Upvotes: 4
Reputation: 86
It's best to keep the flow in the same type from start to finish (in this case a float). You can make a solution using only [f ], [pack], [t b], [t b f], and [route] objects. Maybe a [swap] if you're feeling fancy. The [spigot] object is more useful for not sending messages.
Also, if I understand correctly, you might be having trouble with [toggle] itself. The [toggle] object will be on given any non-zero value and off with a zero value. If you are giving [toggle] a one(1) or a two(2) it will act as if it's on in both cases. You could use [==] to fix this which will output a true(1) or a false(0) but only if you supply an argument like [== 1]. This way two(2) will register as a zero(0), turn [toggle] off, and return your value.
Two examples of alternative solutions:
Honestly though, for less headaches and reusability and simplicity, you should consider picking a pattern in your workflow similar to Pure Data 's inherent true(1) and false(0) paradigm. It will make logic a lot simpler in your future endeavors and is much more similar to the c-logic in which Pure Data is coded and emulates.
This way, if your logic statement accepts or returns a zero(0) as false and a non-zero as true, it will work more 'logically' with other pre-built Pure Data objects. Then your logic might look like the screen below.
More cohesive conditional logic:
I hope it helps!
Upvotes: 2
Reputation: 77
Much simpler solution, and you can set the toggle value to any number, 0 is off, any number larger than 0 is on.
Upvotes: 7
Reputation: 1183
There is more than one way to solve this. Assuming that you want a 1
to be sent out each time the computedVal
changes while toggle
is 1
, it could look like this:
Upvotes: 5