Skip to content

Networking and Communications

Group Assignment

✔️ send a message between two projects

Individual Assignment

✔️ design, build, and connect wired or wireless node(s) with network or bus addresse

For this week, I started building the template for sending and receiving data from Arduino to TouchDesigner via serial port for communication between PC interface and board later.

Sending Data from Arduino to TouchDesigner

In Arduino IDE, write, and upload the following code to the board:

C
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
}
void loop()
{
  delay(1000);

  int  i = 1234;
  Serial.print(i, DEC);
  Serial.print('\n');

  float f = 321.7;
  Serial.print(f);
  Serial.print('\n');
}

In order to receive these messages in TouchDesigner, place down a Serial DAT. By default, all the parameters should be compatible with the above sketch and you should see these two values arriving in the DAT every second.

More specifically, make sure the communication parameters are set to the same baud rate as the Arduino. By default the settings are 9600,8,N,2 but the baud rate can be increased if required. Also, make sure the Table Format is set to One Row Per Line.

Alt text

Receiving Data from Arduino in TouchDesigner

1. In Arduino IDE, write, and upload the following code to the board:

C
//
// Parse incoming messages consisting of three decimal values followed by a carriage return
//  Example  "12 34 56\n"
//  In TouchDesigner:     op('serial1').send("12 34 56", terminator="\n")
//
char buffer[16];   //maximum expected length 
int len = 0;
void setup()
{
  Serial.begin(9600); 
}
void loop()
{
    if (Serial.available() > 0) 
    {
        int incomingByte = Serial.read();
        buffer[len++] = incomingByte;
        //
        // check for overflow
        //
        if (len >= 16)
        {
            // overflow, resetting
            len = 0;
        }
        //
        // check for newline (end of message)
        //
        if (incomingByte == '\n')
        {
            int red, green, blue;
            int n = sscanf(buffer, "%d %d %d", &red, &green, &blue);
            if (n == 3)
            {
                // valid message received, use values here..
            }
            else
            {
                 // parsing error, reject
            }
            len = 0; // reset buffer counter
        }
    }
}

2. TouchDesigner Setup

Alt text

I use the following Chop Execute to send data from any input to the serial DAT in order to send to the board.

Python
# me - this DAT
# 
# channel - the Channel object which has changed
# sampleIndex - the index of the changed sample
# val - the numeric value of the changed sample
# prev - the previous sample value
# 
# Make sure the corresponding toggle is enabled in the CHOP Execute DAT.

def onOffToOn(channel, sampleIndex, val, prev):
    return

def whileOn(channel, sampleIndex, val, prev):
    return

def onOnToOff(channel, sampleIndex, val, prev):
    return

def whileOff(channel, sampleIndex, val, prev):
    return

def onValueChange(channel, sampleIndex, val, prev):
    op('serial1').send(int(val), terminator='\n')
    print(val)
    return

Resources

⬇️ Download Project Files and Assets