I'm using an ultrasonic range finder (HC-SRO4) which I have connected, via an Arduino and my computer’s serial port, to code in Supercollider. I have a Bela Mini and I'd like to use it with the HC-SRO4 and no computer.

a. Can Supercollider somehow run simultaneously with the Bela IDE so that I can use Bela IDE code for running the HC-SRO4 (https://forum.bela.io/d/283-how-do-i-connect-a-hc-sr04-ultrasonic-sensor) and have Supercollider somehow access its distance values as they are output?

b. How hard would it be to translate Bela IDE code for running the HC-SRO4 into Supercollider code in a way that would make the distance values accessible to the Supercollider code I have already written?

c. I’ve looked at the Bela Forum posts regarding connecting an Arduino to Bela via a serial port and using Supercollider. It looks like there has been some success with this but no clear solutions are yet available?
https://forum.bela.io/d/433-connecting-to-serial-devices-with-supercollider
https://tai-studio.org/2016/08/12/bela-capsense.html

Thanks very much for any suggestions!

    this should work for serial communication.

    If you want to interface the HC-SR04 directly to Supercollider, one options is to create a dedicated ugen for it, which requires programming in C++, wrapping the Bela library for the sensor. Another option is to use the existing Bela ugens from sclang to toggle an output pin and measure the response time.

      Great, thanks very much for your reply.

      4 years later

      Can't be done in sclang, because µs-accurate timing is important for this application and sclang runs at a much higher resolution.

      In that Pd example, we are hard resetting a ramp oscillator when the pulse is sent to the TRIG pin and sampling its value when the ECHO pin goes high. We send the TRIG pulse at regular intervals. This allows sample-accurate measurements, so you have a resolution of about 22.7us (1/44100Hz). Using the equation time[us] / 58 = distance[cm] , this gives a nominal resolution of 22.7/58 = 0.39cm, with minimal jitter. Not bad.

      I don't know much about supercollider at all, but I think in scsynth there is (perhaps informally) the concept of "trigger", i.e.: some audio-rate signals that are either 0 or 1 and that some ugens accept such a signal to "trigger" some events. Using the DigitalOut / DigitalIn / DigitalIo ugens at audio rate you could attempt a similar approach, if you have a ramp generator and a sample-and-hold that can handle a trigger input, then you could send into their trigger input the same value sent to the DigitalOut that sends to the TRIG pin and the DigitalIn that receives from the ECHO pin. If you only have a ramp generator that takes a trigger input for reset , but no sample and hold, then you could feed both signals summed together into the trigger input of the ramp generator and then send its output to an envelope detector.