From 5bc0a5c796295398096eae964df51edeb07e3a02 Mon Sep 17 00:00:00 2001 From: "J.M. Dana" Date: Tue, 6 Oct 2015 23:42:19 +0100 Subject: [PATCH 1/2] Divisions turned into multiplications As correctly stated by Mausy5043, the division is quite slow in most microcontrollers, so I have replaced them with multiplications. --- DHT.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index bdd8161..1d188bd 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -46,7 +46,7 @@ float DHT::readTemperature(bool S, bool force) { f = data[2] & 0x7F; f *= 256; f += data[3]; - f /= 10; + f *= 0.1; if (data[2] & 0x80) { f *= -1; } @@ -60,11 +60,11 @@ float DHT::readTemperature(bool S, bool force) { } float DHT::convertCtoF(float c) { - return c * 9 / 5 + 32; + return c * 1.8 + 32; } float DHT::convertFtoC(float f) { - return (f - 32) * 5 / 9; + return (f - 32) * 0.55555; } float DHT::readHumidity(bool force) { @@ -79,7 +79,7 @@ float DHT::readHumidity(bool force) { f = data[0]; f *= 256; f += data[1]; - f /= 10; + f *= 0.1; break; } } @@ -194,11 +194,11 @@ boolean DHT::read(bool force) { _lastresult = false; return _lastresult; } - data[i/8] <<= 1; + data[i * 0.125] <<= 1; // Now compare the low and high cycle times to see if the bit is a 0 or 1. if (highCycles > lowCycles) { // High cycles are greater than 50us low cycle count, must be a 1. - data[i/8] |= 1; + data[i * 0.125] |= 1; } // Else high cycles are less than (or equal to, a weird case) the 50us low // cycle count so this must be a zero. Nothing needs to be changed in the From ce3190f65c8710c8e6ed83c97ea055c2626339a1 Mon Sep 17 00:00:00 2001 From: Tony DiCola Date: Mon, 12 Oct 2015 18:37:54 -0700 Subject: [PATCH 2/2] Switch back to integer division in array indexing. --- DHT.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index 1d188bd..ec57de5 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -194,11 +194,11 @@ boolean DHT::read(bool force) { _lastresult = false; return _lastresult; } - data[i * 0.125] <<= 1; + data[i/8] <<= 1; // Now compare the low and high cycle times to see if the bit is a 0 or 1. if (highCycles > lowCycles) { // High cycles are greater than 50us low cycle count, must be a 1. - data[i * 0.125] |= 1; + data[i/8] |= 1; } // Else high cycles are less than (or equal to, a weird case) the 50us low // cycle count so this must be a zero. Nothing needs to be changed in the