String Data Parsing - Touchdesigner to Arduino to MG995 Servos

Hello all. I am attempting to parse through a string I'm sending from Touchdesigner to an Arduino Uno. The string contains data from 5 channels (currently, would like to eventually have this be 25) and I'm attempting to get Arduino to parse through the string and assign the values from each channel individually to one of Arduinos pwm outputs to a MG995 Servo.

Currently, I am able to communicate with one servo, but the rotational values aren't being fully realized. Sometimes the second output will get data from the first outputs channel and vice versa. There seem to be some bugs - Any suggestions on the code?

//servo motor control from TouchDesigner
//Here is the Arduino code:
//Make sure the serial OP Active parameter is Off before uploading
//commented code can be used instead if you want to dim an LED on pin 9

#include <Servo.h>
//Servo myservo;  // create servo object to control a servo

#define SERVOS 5

uint8_t myBuffer[SERVOS];   //maximum expected length
int pos = 0;
const int servoPins[5] = {3, 5, 6, 9, 10};
//uint8_t servoNumber = 0;


void setup()
{
  Serial.begin(115200);
  pinMode(servoPins[0], OUTPUT);

}
void loop()
{

  while (Serial.available() > 0)
  {
    for (uint8_t numServo = 0; numServo < SERVOS; numServo++) //Go through my Serial data, pop off however many integers I expect
    {
      myBuffer[numServo] = Serial.parseInt();
    }
    
    char r;
    do {
      r = Serial.read();
    }while(r != '\n'); //Dump the rest of the buffer in the trash
    
    for (uint8_t numServo = 0; numServo < SERVOS; numServo++) //Write data to pins (will need ot change for actual servo implementation)
    {
      analogWrite(servoPins[numServo], myBuffer[numServo]);
    }
  }
}

That code will not work with even 1 servo.

You need to create instances (construct) of the servos. For 1 servo:

Servo servo;
Servo servo[5]; // create an array of servos

You need to attach the servo.

servo.attach(3);

Servos do not use the analogWrite function. They use servo.write(degrees) or servo.writeMicroseconds(us).

I suggest that you look at the serial input basics tutorial for ideas on reading serial input and parsing the data.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.