How to use multiple ultrasonic sensors connected to one arduino in TD

It’s my first time using an arduino with multiple ultrasonic sensors in TouchDesigner. I know how to get data from one ultrasonic sensor to make for example a SOP-Sphere move along with the data, using the serial and select operators.

I want to use 4 ultrasonic sensors connected to one arduino separetely in Touchdesigner. I have the arduino code completed and it works perfectly fine, but I want to create 4 different objects in TouchDesigner using the data from one of the 4 sensors. But I dont know what I have to do in the serial and select operators to only get the data from one of the sensors, for each object.

For example:
Object 1 (SOP-Sphere) = getting data from sensor 1
Object 2 (SOP-Rectangle) = getting data from sensor 2
Object 3 (SOP-Noise) = getting data from sensor 3
Object 4 (SOP-Sphere = getting data from sensor 4

It depend on how you receive information from sensor in Arduino
But I wrote an Arduino program able to send multiple digital and analog signal to TD:

 void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pins 2 as input and enable the internal pull-up resistor
  for (int i = 2 ; i <= 13; i++){
  pinMode(i, INPUT_PULLUP);
  }
}

void loop() {
  //read the pushbutton value into a variable
  String sendVal = "v";
  for (int i = 2 ; i <= 13; i++){
  sendVal += digitalRead(i);
  }
  //print out the value of the pushbutton
  Serial.println(sendVal);
  //read analog into variable and print it
  
  String A = "A";
  A += analogRead(A0);
  Serial.println(A);
  String B = "B";
  B += analogRead(A1);
  Serial.println(B);
  String C = "C";
  C += analogRead(A2);
  Serial.println(C);
  String D = "D";
  D += analogRead(A3);
  Serial.println(D);
  String E = "E";
  E += analogRead(A4);
  Serial.println(E);
  String F = "F";
  F += analogRead(A5);
  Serial.println(F);
  
  delay(10);
}

and the program on the serial callback

def onReceive(dat, rowIndex, message, bytes):
	if message[0] == 'v':
		op('table1').clear()
		flow = list(message)
		op('table1').appendCol(flow[1:])
	elif message[0] == 'A':
		value = message[1:]
		op('table2')['A',1] = float(value)/1023
	elif message[0] == 'B':
		value = message[1:]
		op('table2')['B',1] = float(value)/1023
	elif message[0] == 'C':
		value = message[1:]
		op('table2')['C',1] = float(value)/1023
	elif message[0] == 'D':
		value = message[1:]
		op('table2')['D',1] = float(value)/1023
	elif message[0] == 'E':
		value = message[1:]
		op('table2')['E',1] = float(value)/1023
	elif message[0] == 'F':
		value = message[1:]
		op('table2')['F',1] = float(value)/1023
	return

And I have the separate value in different tables.

Oh I see! Thank you for your help.

I dont know if I could somehow do the same thing as you with my current arduino code, or if I have to completely change the arduino code.

This is the code I´m using right now:

// trigger/echo pin pairs.
const byte rangers [][2] { {9, 10}, {5,4}, {12, 13}, {7, 6}};

void setup()
{
  for (auto& ranger : rangers) {
    pinMode(ranger [0], OUTPUT);
    digitalWrite (ranger [0], LOW);
    pinMode(ranger [1], INPUT);
  }
  Serial.begin(9600); 
}

int range (const byte ranger [])
{
  digitalWrite(ranger [0], HIGH);
  delayMicroseconds (10);
  digitalWrite(ranger [0], LOW);
  unsigned long duration = pulseIn(ranger [1], HIGH);
  return duration / 58.2;
}

void loop() 
{
  for (int i = 0; i < 4; i++) {
    int distance = range (rangers [i]);
    
    if(distance > 0 && distance < 50){
      Serial.print(distance);
    }
    Serial.println ();
    delay (50); // Ping no more frequently than 20 Hz
  }  
}

You need to test with real devices:
I would use that on Arduino:


// trigger/echo pin pairs.
const byte rangers [][2] { {9, 10}, {5, 4}, {12, 13}, {7, 6}};

void setup()
{
  for (auto& ranger : rangers) {
    pinMode(ranger [0], OUTPUT);
    digitalWrite (ranger [0], LOW);
    pinMode(ranger [1], INPUT);
  }
  Serial.begin(9600);
}

int range (const byte ranger [])
{
  digitalWrite(ranger [0], HIGH);
  delayMicroseconds (10);
  digitalWrite(ranger [0], LOW);
  unsigned long duration = pulseIn(ranger [1], HIGH);
  return duration / 58.2;
}

void loop()
{
  int distance = range (rangers [0]);
  String A = "A";
  A += distance;
  Serial.println(A);
  distance = range (rangers [1]);
  String B = "B";
  B += distance;
  Serial.println(B);
  distance = range (rangers [2]);
  String C = "C";
  C += distance;
  Serial.println(C);
  distance = range (rangers [3]);
  String D = "D";
  D += distance;
  Serial.println(D);
  delay (10); // Ping no more frequently than 20 Hz
}

and that on callback

def onReceive(dat, rowIndex, message, bytes):
	if message[0] == 'A':
		value = message[1:]
		op('table2')['A',1] = float(value)/1023
	elif message[0] == 'B':
		value = message[1:]
		op('table2')['B',1] = float(value)/1023
	elif message[0] == 'C':
		value = message[1:]
		op('table2')['C',1] = float(value)/1023
	elif message[0] == 'D':
		value = message[1:]
		op('table2')['D',1] = float(value)/1023
	return

Thank you very much for your reply. Appreciate it!