Error in python code, serial DAT

I’m a beginner and I’m working on a project in TouchDesigner using 4 ultrasonic sensors connected to one arduino.
I want to get the distances from the sensors separetely to send data to 4 objects. For example:

Object 1 (SOP-Sphere) = getting data from sensor A
Object 2 (SOP-Rectangle) = getting data from sensor B
Object 3 (SOP-Noise) = getting data from sensor C
Object 4 (SOP-Sphere = getting data from sensor D

So for example in the Serial/Select operator it has to show:

A = 34
B = 23
C = 5
D = 45
A = 9
B = 33
C = 12
D = 8

I’m getting an error in the python code I’m using in the serial DAT. And I dont know what’s going wrong.

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 the python code I’m using in TouchDesigner

def onReceive(dat, rowIndex, message, bytes):
  if message[0] == 'i':
    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 the error code I’m getting in the python operator:

Does anyone maybe knows what I have to change in the python code?

the Python error IndexError: string index out of range means that the index you are trying to access, does not exist in a string. For instance if the string is empty.

To see why you get this error, we need to see what the value of message is when the error occurs.
You can debug print message like this:

def onReceive(dat, rowIndex, message, bytes):
    debug("message=", message)
    if message[0] == 'i':
     ...<continue your code here>

Now to see this output, the easiest way is to split your pane in two, and set the second pane to be a textport. Look for the the printed lines in textport which tell you what the value of message is.

Thank you very much for your reply.

I´m getting these messages (It only happens when I move my hand in front of the ultrasonic sensors, and the distances changes because of the movement):

I don’t know if I did something wrong in the parameters, but the arduino code works perfectly fine in the serial monitor, so maybe I did something wrong in the Touchdesigner parameters of the serial DAT. Is there something specific I have to change in the parameters in order for the python code to work?

Python expects the code to have correct indentation (=the amount of spaces/tabs at the beginning of each line of code). It does not matter if you use spaces or tabs, but you must use the same kind in the whole script.
If you read the errors there is a wrong indentation in the serial1_callbacks DAT node in TD, it also gives you the line number (14) where the problem occurs. I guess this happened when you copy pasted code into it or made a typo. Should be an easy fix!

Also see: Indentation in Python - AskPython

Thank you!!