How to use multiple distance sensors (connected to Arduino) in TouchDesigner?

First Arduino project so might need some help! I have successfully got data from one distance sensor into Touch Designer, however the next step I want to do it use two. I have them both set up and working in Arduino sketch, but don’t know how to separate the data to go into Touch Designer. So one sensor can control one parameter and the other can control another - can’t seem to separate them. through the serial and select operators. Any advice or suggestions would be appreciated!

Here is my Arduino code (most likely far from perfect):

// ULTRASONIC SENSOR
int TRIG = 3;
int ECHO = 2;
int DURATION;
int DISTANCE;

int TRIG_2 = 8;
int ECHO_2 = 7;
int DURATION_2;
int DISTANCE_2;

void setup() {

// ULTRASONIC SENSOR
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);

pinMode(TRIG_2, OUTPUT);
pinMode(ECHO_2, INPUT);

// SERIAL
Serial.begin(9600);

}

void loop() {
//LEFT SENSOR
digitalWrite(TRIG,HIGH);
delay(1);
digitalWrite(TRIG,LOW);
DURATION = pulseIn(ECHO,HIGH);
DISTANCE = DURATION / 58.2;

if(DISTANCE > 0 && DISTANCE < 50 )
Serial.println(DISTANCE);
delay(100);

//RIGHT SENSOR
digitalWrite(TRIG_2,HIGH);
delay(1);
digitalWrite(TRIG_2,LOW);
DURATION_2 = pulseIn(ECHO_2,HIGH);
DISTANCE_2 = DURATION_2 / 58.2;

if(DISTANCE_2 > 0 && DISTANCE_2 < 50 ){
Serial.println(DISTANCE_2);
delay(100);
}

}

Hello and welcome to the forum!

try to send both values in the same serial print line separated by a “tab” /t, something like

    Serial.print(DISTANCE);
    Serial.print("\t");
    Serial.println(DISTANCE_2);

then in touchDesigner you can receive this in the serial DAT and separate by /t using a Convert DAT. Then you can select each column independently with its sensor value using a Select DAT, and send each value to control different parts of your network.

I don’t have an Arduino with me to test this but from memory that’s the workflow more or less.

I hope this helps to get you going!

I had a project like this and I also output both values to the same line but I used a pipe | to separate it. Then in python i used split command to break it into parts.

sensorData = "123|657"
distances = sensorData.split("|")
print(distances[0])
print(distances[1])
1 Like