// example code by C. Goulding (AKA groundFungus)
// credit to Robin2, author of the serial input basics tutorial
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
float azimuth;
float elevation;
float pressure;
void setup()
{
Serial.begin(9600);
Serial.println("<Arduino is ready> Enter Azimuth and Elevation separated with a semicolon (;) ");
}
void loop()
{
recvWithEndMarker();
//showNewData();
if (newData)
{
parseData();
}
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
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] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData()
{
if (newData == true)
{
Serial.print("This just in ... ");
Serial.println(receivedChars);
//newData = false;
}
}
void parseData()
{
char *strings[32]; // an array of pointers to the pieces of the above array after strtok()
char *ptr = NULL; byte index = 0;
ptr = strtok(receivedChars, ";"); // delimiters, semicolon
while (ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, ";");
}
Serial.println(index);
// print all the parts
Serial.println("The Pieces separated by strtok()");
for (int n = 0; n < index; n++)
{
Serial.print("piece ");
Serial.print(n);
Serial.print(" = ");
Serial.println(strings[n]);
}
// convert string data to float numbers
azimuth = atof(strings[0]);
elevation = atof(strings[1]);
pressure= atof(strings[2]);
newData = false;
Serial.print("Azimuth = ");
Serial.print(azimuth, 3);
Serial.print(" Elevation = ");
Serial.print(elevation, 3);
Serial.print(" Pressure = ");
Serial.print(pressure, 3);
Serial.println(); // blank line
}