Hello all,
Just wondering if I could ask for a little help please.
I’m exporting three values from Unreal to drive the functionality of servos and LEDs in an Arduino. The variables are formatted as <a, b, c>. Currently, there’s no response from the Arduino. Any insights on the possible reasons behind this issue would be greatly appreciated.
Here’s the blueprint of how the values are being printed: Imgur: The magic of the Internet
Here’s my Arduino code:
// For now there’s only 1 servo being used as a test
#include <Servo.h> // servo library
Servo servo1; // servo control object
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = { 0 };
int integerFromPC1 = 0;
int integerFromPC2 = 0;
int integerFromPC3 = 0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
servo1.attach(7);
/*Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();*/
servo1.write(40);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
servo1.write(map(integerFromPC2, 0, 6, 0, 180));
delay(100);
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(integerFromPC1, strtokIndx);
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
integerFromPC3 = atoi(strtokIndx); // convert this part to an int*/
}
//============
void showParsedData() {
Serial.print("Integer1 ");
Serial.println(integerFromPC1);
Serial.print("Integer2 ");
Serial.println(integerFromPC2);
Serial.print("Integer3 ");
Serial.println(integerFromPC3);
}