We are trying to receive a string that will give values like 1473,1492,1100,0,1; from a raspberry pi and take that data and assign the first number (1473) to heave, the second to surge, etc. Attached is the code we have so far, suggestions please!
const byte numChars = 20;
char receivedChars[numChars]; // an array to store the received data
int heave;
int sway;
int surge;
int pan_left;
int pan_right;
boolean newData = false;
void setup() {
Serial.begin(9600);
}
void loop() {
//This code will continuously send ‘’ python.
//Serial.println("");
delay(400);// must be added.
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ‘;’;
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = ‘;’; // terminate the string
ndx = 0;
newData = true;
}
heave = receivedChars[1];
sway = receivedChars[2];
surge = receivedChars[3];
pan_left = receivedChars[4];
pan_right = receivedChars[5];
}
}
void showNewData() {
if (newData == true) {
Serial.println("This just in … ");
Serial.println(receivedChars);
Serial.println("Surge = ");
Serial.println(surge);
Serial.println("Heave = ");
Serial.println(heave);
Serial.println("Sway = ");
Serial.println(sway);
newData = false;
}
}