TouchDesigner Send Data to Arduino — Tutorial in TouchDesigner

TouchDesigner connect Arduino : Arduino Receive Data from TouchDesigner sending
TouchDesigner Side
Like sending data from Arduino to TouchDesigner. Get a Serial (DAT) first. And I used the One Per Byte here because of the case needed. (In the Serial (DAT) panel, the parameter “Row/Callback Format”)
Also, need to setup Port & Serial Baud Rate. You can refer

Script of sending data out.
def onStart():
op(‘forArduinoOutput’).send('n', terminator='\n')
return
I used onStart() function because I link Execute (DAT) to trigger that script.
“forArduinoOutput” is my Serial (DAT) name, and using send() to send the data out.
“n” is what string you want to send. “terminator=’\n’ ” let receiver knows the string is end and create a new line.

Arduino Side
Full Code
void setup() {
Serial.begin(9600);
}void loop() {
String s = "";
while (Serial.available()) {
char c = Serial.read();
if (c != '\n') {
s += c;
}
}
if (s != "") {
if (s == "word") {
// Execute Something
}
}
}
Explain
We setup Serial Baud Rate “9600” first.
void setup() {
Serial.begin(9600);
}
In the loop() function, we have to checking the Serial whether connect or not.
while (Serial.available()) {}
Once connected, reading the data from Serial. But now we got are the byte data. We have to re-connect them to be a string. And adding if to know where is the end of string. That’s why we add “\n” to the all string in TouchDesigner.
char c = Serial.read();
if (c != '\n') {
s += c;
}
Last step, you can use the data to run any script you want. For example, I checked if the data equal to “word”, then execute something.
if (s != "") {
if (s == "word") {
// Execute Something
}
}