From 585e992a4f1e38b4d3ec519d436ea6059b4b6793 Mon Sep 17 00:00:00 2001 From: EmbeddedDevver Date: Thu, 23 Sep 2021 20:55:12 +0200 Subject: [PATCH 1/2] Unnecessary long delay during receiving. The reason why you needed to include the 15mS timer, is because the while loop executes very fast and the data from the Ikea module will be received slowly. Thus the while loop exits too early if you do not had the 15mS delay included because there was no data (yet) inside the UART buffer. But your 15mS delay causes that for every received character, there will be 15mS delay. So 64 characters received will be 64 times 15mS equals a a long (blocking) code. It's better to use this code, where I use a timeout for the entire frame and not for every character. --- src/SerialCom.h | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/SerialCom.h b/src/SerialCom.h index db77a74..cbca9f0 100644 --- a/src/SerialCom.h +++ b/src/SerialCom.h @@ -82,15 +82,23 @@ namespace SerialCom { } Serial.print("Receiving:"); - while (sensorSerial.available()) { - serialRxBuf[rxBufIdx++] = sensorSerial.read(); - Serial.print("."); - - // Without this delay, receiving data breaks for reasons that are beyond me - delay(15); - - if (rxBufIdx >= 64) { - clearRxBuf(); + long now = millis(); + long lastMsg = now; + // wait for data with a timeout. + while (now - lastMsg < 15) + { + if (sensorSerial->available()) + { + now = millis(); // re-init the timeout timer + lastMsg = now; // since we received new data. + + serialRxBuf[rxBufIdx++] = sensorSerial->read(); + Serial.print("."); + + if (rxBufIdx >= 64) + { + clearRxBuf(); + } } } Serial.println("Done."); From a5ee0dce7da0e21f26a0b16ee02e706af43b5fd5 Mon Sep 17 00:00:00 2001 From: EmbeddedDevver Date: Fri, 24 Sep 2021 08:00:54 +0200 Subject: [PATCH 2/2] Update SerialCom.h --- src/SerialCom.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/SerialCom.h b/src/SerialCom.h index cbca9f0..442ef5b 100644 --- a/src/SerialCom.h +++ b/src/SerialCom.h @@ -85,12 +85,11 @@ namespace SerialCom { long now = millis(); long lastMsg = now; // wait for data with a timeout. - while (now - lastMsg < 15) + while (millis() - lastMsg < 15) { if (sensorSerial->available()) { - now = millis(); // re-init the timeout timer - lastMsg = now; // since we received new data. + lastMsg = millis(); // re-init the timeout timer since we received new data. serialRxBuf[rxBufIdx++] = sensorSerial->read(); Serial.print(".");