How do I name 4 ultrasonic sensors A, B, C, D in code

Hello, I´m new to using Arduino and I want to use 4 ultrasonic sensors in a program called TouchDesigner. For TouchDesigner I have to code in python for it to recognize 4 different sensors, so in my Arduino code I have to name them A, B, C, D.
I think I have to do something with String and digitalRead. I tried to do this myself but it doesn't work and I cant figure out what I'm doing wrong.
For example:

  String A = "A";
  A += digitalRead();
  Serial.println(A);
  String B = "B";
  B += digitalRead();
  Serial.println(B);
  String C = "C";
  C += digitalRead();
  Serial.println(C);
  String D = "D";
  D += digitalRead();
  Serial.println(D);

The only thing I have to do is naming the sensors A, B, C and D. But I dont know how to do this in the arduino 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
  }  
}

I will try to give a more clear discription of what I'm trying to achieve.
I have 4 ultrasonic sensors to measure the distance (each sensor separetely) between 0 and 50. In the Program TouchDesigner (for visual programming) I have four 3d objects moving along with the distances from the sensors

Object 1 = getting data from sensor A
Object 2 = getting data from sensor B
Object 3 = getting data from sensor C
Object 4 = getting data from sensor D

I'm still learning how arduino coding and TouchDesigner works, but I'm working on an art project for school. So bear with me while I'm trying to explain what I'm trying to do.
Someone from the TouchDesigner community sent me his code, what he was using himself for his project. I dont know exactly what his project was about, but he was explaining to name the sensors A, B, C and D in the Arduino code. and he sent me his phyton code for TouchDesigner.
I'm trying to achieve something like that, but with 4 ultrasonic sensors, measuring the distance between 0 and 50.

The arduino code he was using:

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

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 += digitalRead(9);
  Serial.println(A);
  String B = "B";
  B += digitalRead(5);
  Serial.println(B);
  String C = "C";
  C += digitalRead(12);
  Serial.println(C);
  String D = "D";
  D += digitalRead(7);
  Serial.println(D);

  delay(10);
}

And the python code he was using in TouchDesigner

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

I need to get the distances from the sensor A, B, C and D, measuring only between 0 and 50.
Then I need to use python in TouchDesigner for it to recognize the sensors separetely, that's why the sensors has to be A, B, C and D. if I understand correctly.

XY problem here.

take a step back and explain what you are trying to achieve. Your Arduino code does ranging across 4 'rangers' and print out the distance.

if you want to see the ID of the 'ranger' that printed that distance you could do

void loop() 
{
  for (int i = 0; i < 4; i++) {
    int distance = range (rangers [i]);
    
    if(distance > 0 && distance < 50){
      Serial.write('A'+i); Serial.write('\t'); Serial.print(distance); // PRINT A,B,C or D before the distance
    }
    Serial.println ();
    delay (50); // Ping no more frequently than 20 Hz
  }  
}

Serial.print ('A' + i);

If you just need to produce a letter A-D from an index 0-3, you can

char sensorName = sensorIndex + 'A';

Hi Daiisy1,

You are welcome on this forum!
best regards Stefan

You're using advanced C++ construct 'auto&', yet you don't use the index value anywhere in the statement. Instead you hard coded the first index value (and for some strange reason, the second as well) which won't change in successive iterations.

Also, you might think the the String class will help you construct your message. This is probably not the case. However as mentioned above, you haven't explained your application very well so it's difficult to know.

That would be my fault.

11 posts were split to a new topic: What information do we need to answer a question effectively?

I gave extra info in my description. Hopefully it's more clear what I'm trying to achieve.

try answer #2

may be instead of a tab you need another type of separator and may be no separator at all as your python code does

  elif message[0] == 'A':
    value = message[1:]

which means if the first character I get is 'A' then the value is whatever is in the message buffer starting at index 1 (second character)

distances are not read through a digitalRead on the sensor pin.

Thank you. It might be a delicate time to point it out, but you shouldn't edit previous posts except to correct formatting. It destroys the meaningfulness of the replies.

The arduino code is now showing A, B, C and D. So that's good. Thank you.
I'm going to check right now if it's going to work in TouchDesigner.

Okay thanks for letting me know. I'll keep that in mind.

try this if there is an issue

void loop() 
{
  for (int i = 0; i < 4; i++) {
    int distance = range (rangers [i]);
    
    if(distance > 0 && distance < 50){
      Serial.write('A'+i); Serial.print(distance); // PRINT A,B,C or D before the distance
    }
    Serial.println ();
    delay (50); // Ping no more frequently than 20 Hz
  }  
}

I removed the tab between the letter and the distance.

Also don't keep the Serial monitor open if you use the Python code. you can't have both open at the same time

It doesn't work in TouchDesigner, but I think I'm doing something wrong in the python code
This is what I'm using

The arduino code I'm using:

// 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.write('A'+i); Serial.print(distance); // PRINT A,B,C or D before the distance
    }
    Serial.println ();
    delay (50); // Ping no more frequently than 20 Hz
  }  
}

and this the python code I'm using in TouchDesigner:

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
  return

This is what TouchDesigner is showing (I need to get the numbers without the letters in the right "select" operator, but it still shows the letters and numbers

This is the error code in TouchDesigner

I dont know if someone can understand what is going wrong. P.s. sorry for the bad quality pictures, I can't take screenshots in TouchDesigner

do you know what this means ?
image

seems what it got from serial1 ?
what are the 5 lines about? I see an A in #4 but nothing in 3 and 2

can you print message in the python code to a Serial monitor in TouchDesigner? It would be interesting to see if we really got what we think it is.

also
op('table2')['A',1] = float(value)/1023
why do you divide by 1023 if you are sending a distance that is between 0 and 50?
is that what's expected or is it code from another example where an analogRead() value had been sent and you needed something between 0 and 1?

may be there is documentation on how to format the communication?

Sure it will. It is a 2 dimensional array so each iteration, ranger is a 2 element array, output pin/input pin.

Oh, I overlooked the two dimensional nature of the array. Right.

I have never seen touch-designer before. So I don't know how it is working.

oops you added new info in post #1. Ok reading it.....

Aha. Well I know just a little bit about python. I wrote three small scripts that do some string-extraction and string-manipulation. That is what the python-code of the original code is doing.
hm ... --- ... I'm thinking about what analogon to use to explain the situation you are in.....

Think of a person from year 1650. This person has no idea about what a motor-cycle is because they don't exist in the year 1650. What comes "nearest" to a motor-cycle is a horse.

Imagine this person is time-transferred into 2021
So this person sees a motor-cycle and has seen somebody getting on the motor-cycle "jump" on the kick-starter motor starts and then rides away.

This person tries to imitate the rider based on his knowledge (which is just about horses)
So he might get on the bike taking his foot to smack it against the backwheel trying to start the motor. (Just like you would do it with a horse to make it gallop)

He is trying this based on his knowledge which is too less to understand how it works to start a motorcycle.

He would have to learn:

  • open the fueltap,
  • put in key and turn the key to position "ignition on"
  • change gear to neutral or pull clutch
  • put your foot onto the kickstarter and jump down in this particular manner (showing it)

a good amount of new knowledge is needed to make it work.
It is similar with programming. You need quite some amount of new knowledge how the details of python using things like

message[0] == 'B':

really mean and do to be able to adapt them to your code
same thing on the Arduino side

The way to learn this is to practice with really small code-examples.
They have to be small to stay understandable what is going on.

So if somebody knows of a good python introducing tutorial please post it.

This forum - and I'm sure there is a similar forum for python - will support you.
As long as you show some own effort (like you already did by posting code as code-sections well done !) you will get support

best regards Stefan

between the lines you'll see the values from the ultasonic sensors. In the picture it was not showing the correct values because of the error in python code.

This is how it shows the correct information, when it only has one ultrasonic sensor connected (I dont have to use python when I'm only using one sensor, so I know how to do this)

to use multiple sensors, you have to use python in order for the program to recognize the different sensors.

Also, the code with divide by 1023 was from another example.