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]);
}
}
}