3

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;
Max N
  • 1,134
  • 11
  • 23
GingerDom
  • 113
  • 1
  • 1
  • 9

4 Answers4

7

Much simpler solution, and you can set the toggle value to any number, 0 is off, any number larger than 0 is on.

Solution

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Radiowaves
  • 77
  • 1
  • 3
  • simpler than which solution? also, so far *every* answer to this question had "0 is *off*, any other number is *on*" – umläute Jun 21 '17 at 21:59
  • 2
    @umläute than everything containing more than 4 objects like here - expressions are the most easiest to read, don't need help to resolve unknown blocks and no externals also not falling into following wires and hot-cold inlet logic... they are easier for everybody that used to learn other languages like procedural, structural, objective, functional etc. all non-flow-based ;-) – Sebastian Xawery Wiśniowiecki Sep 25 '20 at 15:47
5

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:

conditional statement

Max N
  • 1,134
  • 11
  • 23
4

Here's a solution using a demultiplexer idiom:

enter image description here

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.

umläute
  • 28,885
  • 9
  • 68
  • 122
  • [list prepend 0] does not work in Pure Data vanilla. Is it provided by some external? – bmitc Mar 05 '23 at 15:29
  • it does work with Pd vanilla since Pd-0.40 or so. It is hard to imagine that you use a version so old, so you probably just did a typo. – umläute Mar 06 '23 at 07:13
  • Very strange. Thank you for responding because I tried again today, and it worked fine. I know I didn't make a typo in Pure Data before I made my previous comment, as I tried it several times, so Pure Data must have been in a weird state. I'm on the latest version. – bmitc Mar 07 '23 at 05:51
2

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:

two 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:

more cohesive conditional logic

I hope it helps!

taylor vann
  • 86
  • 2
  • 5