As per your suggestion I have turned my code into:
Sender code:
void setup() {
Serial.begin(9600);
}
void loop() {
int value=analogRead(A0);
Serial.print('<'); // start marker
Serial.println(value);
Serial.print(','); // comma separator
Serial.println('>'); // end marker
delay (500);
}
and receiver code
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("");
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
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 showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
This seems to work, thank you very much! I just have a question concerning the code in example 3, since i am not very good at programming:
the function recvwithStartEndMarkers() is quite complex, would you mind breaking it down a bit so I can understand it? I am really struggling to understand it...