Hello fellow TD gurus,
I’ve got some problems sending data from Touchdesigner to the Arduino IDE. My project is about controlling WS2811 LEDs using TD. I’m trying to send <“led_nr” , “color_r” , “color_b” , “color_g”> trough a single line serial message. But somehow only the first value is gotten by the IDE.
Here is the .py file from TD, wich is executed at every frames start:
nr = op('led_nr').par.value0
red = op('red_value').par.value0
green = op('green_value').par.value0
blue = op('red_value').par.value0
op('serial1').send('<', nr, red, blue, green, '>' , terminator="\n")
And here is the Arduino project file:
#include <FastLED.h>
//Serial comm setup:
const byte numChars = 32;
char recievedChars[numChars];
char tempChars[numChars];
//Data got from serial
int led_nr;
int r_val;
int g_val;
int b_val;
boolean newData = false;
// How many leds in your strip?
#define NUM_LEDS 120
#define DATA_PIN 7
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
Serial.println("Please close this serial window in order to use it with TD");
Serial.println();
FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
recvWithStartEndMarkers();
if (newData == true){
strcpy(tempChars, recievedChars);
parseData();
showParsedData();
leds[led_nr] = CRGB(r_val,g_val,b_val);
FastLED.show();
newData = false;
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while(Serial.available() > 0 and newData == false){
rc = Serial.read();
if(recvInProgress == true){
if(rc != endMarker){
recievedChars[ndx] = rc;
ndx++;
if(ndx >=numChars){
ndx= numChars-1;
}
}
else {
recievedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if(rc == startMarker){
recvInProgress = true;
}
}
}
void parseData(){
char * strtokIndx;
strtokIndx = strtok(tempChars,",");
led_nr = atoi(strtokIndx);
strtokIndx = strtok(NULL,",");
r_val = atoi(strtokIndx);
strtokIndx = strtok(NULL,",");
g_val = atoi(strtokIndx);
strtokIndx = strtok(NULL,",");
b_val = atoi(strtokIndx);
}
void showParsedData(){
Serial.print("Led nr.: ");
Serial.print(led_nr);
Serial.print(" ");
Serial.print("Color: ");
Serial.print(r_val);
Serial.print(",");
Serial.print(g_val);
Serial.print(",");
Serial.print(b_val);
Serial.println();
Serial.print("");
}
Thanks in advance!