state change threshold detection

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

state change threshold detection

brendan
Hallo
I am trying to generate a binary state threshold detector. Using a Slider, and the following code I can only get an output stream of 1s or 0s:

w = Window.new.front;
a = Slider(w, Rect(20, 60, 150, 20))
.action_({
var old1, new1;
if(a.value > 0.5) {new1=1} {new1=0};
if(new1 != old1) {new1.postln; old1=new1};
//a.value.postln
});
)

What I require is a SINGLE 1 when I exceed 0.5, and a single 0 when I pass below 0.5. I'm trying to convert dynamic sensor data into a gate/trigger.

Please help, thanks
Brendan
Reply | Threaded
Open this post in threaded view
|

Re: state change threshold detection

brendan
I've been working at this most of today and have come up with this, which works but only if the Slider value is changing quickly:

(
w = Window.new.front;
~prev = 0;
x = Slider(w, Rect(20, 60, 150, 20))
    .action_({
        //x.value.postln;
        var now = x.value;
        if((now > 0.5) && (~prev < 0.5)){1.postln};
        if((now < 0.5) && (~prev > 0.5)){0.postln};
                ~prev = now;
});
x.action.value;
)
Reply | Threaded
Open this post in threaded view
|

Re: state change threshold detection

brendan
SOLVED:

(
var prev;
w = Window.new.front;
a = Slider(w, Rect(20, 60, 150, 20))
.action_({
var now;
if(a.value > 0.5) {now=1} {now=0};
if(now != prev) {now.postln; prev=now};
});
)