try this (complex) asynchronous approach:
// example packet: t123
const size_t PACKET_SIZE = 3;
template <class T> inline Print& operator <<(Print& obj, T arg)
{
obj.print(arg);
return obj;
}
char SENSOR_THr[3];
void setup()
{
Serial.begin(9600);
Serial << ("let's go!\n");
}
void loop()
{
if (const char* newPacket = checkForNewPacket(Serial, 't'))
{
Serial << (F("Just Recieved:\t")) << newPacket[0] << "," << newPacket[1] << "," << newPacket[2] << "\n";
for (size_t i = 0; i < PACKET_SIZE; i++) {
SENSOR_THr[i] = newPacket[i];
}
for (auto i : SENSOR_THr) {
Serial.print(i, HEX);
Serial.print(",");
}
Serial.println();
}
}
const char* checkForNewPacket(Stream& stream, const char startMarker)
{
static char incomingPacket[PACKET_SIZE] = "";
static bool gotStartMarker = false;
static byte idx = 0;
if (stream.available())
{
if (gotStartMarker) {
incomingPacket[idx++] = stream.read();
if (idx > sizeof(PACKET_SIZE)) {
gotStartMarker = false;
return incomingPacket;
} else {
return nullptr;
}
} else if (stream.peek() == startMarker) {
idx = 0;
gotStartMarker = true;
(void) stream.read();
} else {
(void) stream.read();
}
}
return nullptr;
}