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?