How to check if a value exists in an array

Hello Legends,
I’m building off Robin Newman’s awesome Dual Ultrasonic Theramin

I’m using a single sensor and have divided the osc data stream into various ranges (3 - 30, 31-40, 41-55)
using if statements eg, (if b>2 and b>31) but it tends to throw timing exceptions.

Is there a more efficient way to check if the received distance reading is within a defined range (range 3, 30) similar to ruby’s ‘include?’ method?

Any and all replies appreciated!

footnote: sonic pi was my introduction to code and programming - thank you Sam Aaron!

Hey @jonathongriggs :slight_smile: Welcome to the community!
How about you post some of your code below, and we can check it for any potential issues :+1:

Champ!

see if this makes sense, the received osc value bb if checked against the two statements determines if it plays 60. When I give it only 1 condition it runs fine but with extra step of 2 conditions, i think, is causing it to get behind time. So Ive been looking for alternative ways to check if a number is within a range, looking to ruby for clues… I thought about using an array to define the ranges and check if a value exists within the array.

Blockquote
live_loop :how do
b=sync “/osc:*/play_this/”
bb=b[0]
if bb > 40 and bb <60
play 60
end
sleep 0.05
end

@robin.newman will likely have some more comments, but I am not sure it is just the conditional statements that are the issue. There are a couple of recommended practices when receiving a stream of OSC commands, one of them usually being to place use_real_time at the top of the OSC receiving loop to remove Sonic Pi’s scheduling delay for that loop, as we want the OSC to arrive as quickly as possible. We’d also typically separate the OSC loop from the actual playing of the synth/sample, and communicate between them using the Time-State system, (a kind of global memory store). If you haven’t yet, it’d probably be worth reading through the documentation for get and set - (see Tutorial chapters 10 and 10.1 for those).

1 Like

This really helps, I’ve been mostly groping in the dark for ways to do things. Separating the osc loop from the synth/sample loop is a great idea and I’ll try that immediately, will report back…

Hi Jon… welcome to the madness.

I’m sorry you’ve felt left alone in the dark…

This forum exists purely to help people and
to bring us all together… and it’s also fun to
bounce idea’s off each other.

So please… come here as often as you like,
contribute if you can… or just lurk… it’s all good. :slight_smile:

Regards,

Eli…

2 Likes

Haha thanks Eli, well not entirely in the dark, these forums are gold for ideas :slight_smile:

Sorry you’ve been having problems. I meant to add something yesterday but life was rather busy then.
As has been stated I’d start with adding the use_real_time inside the osc receiving loop.
There are various ways to deal with ranges of input. You can use the ruby include? method (although ruby per-se is not formally supported I find it works OK.) Similarly the ruby case structure is something I’ve also used in such situations. Ive also used multiple if…elsif… statements.
The ultrasonic sensors are not particularly responsive. You will find things are much better if you invest in a ToF (Time of Flight) sensor

Hello!
All good! I’m just exploring all the options I don’t yet know about, it’s been quite the adventure.
I found that running the ultrasonic sensor from the raspi as well as the tons of sonic pi sounds and samples I was using caused constant sensor script crashes, most common error “no echo received”. I troubleshooted various forms and methods for triggering the sensor but it wasn’t as snappy as it needed to be for the sculpture. So I used arduino and ethernet rig to send osc to raspi - which is bulletproof, but still not great past 30cm.

ToF looks ideal! Had no idea that existed :slight_smile: Looks like a solid setup! Enjoyed your vid :slight_smile: I’ll be using that for the next piece!

I included use_real_time in each of the time sensitive loops, and dedicated a loop to posting the latest osc value to Time State as Ethen suggested :slight_smile: which immediately gave results​:+1:

Ah! else if ! I’m going to try that :slight_smile: Here’s a vid that sort of shows what it is. It’s not the final sounds but it gives the idea :slight_smile:

3 Likes

That sounds interesting! The video is of your project?

1 Like