Skip to content

Commit 5bc0a5c

Browse files
committed
Divisions turned into multiplications
As correctly stated by Mausy5043, the division is quite slow in most microcontrollers, so I have replaced them with multiplications.
1 parent 5fb1668 commit 5bc0a5c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

DHT.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ float DHT::readTemperature(bool S, bool force) {
4646
f = data[2] & 0x7F;
4747
f *= 256;
4848
f += data[3];
49-
f /= 10;
49+
f *= 0.1;
5050
if (data[2] & 0x80) {
5151
f *= -1;
5252
}
@@ -60,11 +60,11 @@ float DHT::readTemperature(bool S, bool force) {
6060
}
6161

6262
float DHT::convertCtoF(float c) {
63-
return c * 9 / 5 + 32;
63+
return c * 1.8 + 32;
6464
}
6565

6666
float DHT::convertFtoC(float f) {
67-
return (f - 32) * 5 / 9;
67+
return (f - 32) * 0.55555;
6868
}
6969

7070
float DHT::readHumidity(bool force) {
@@ -79,7 +79,7 @@ float DHT::readHumidity(bool force) {
7979
f = data[0];
8080
f *= 256;
8181
f += data[1];
82-
f /= 10;
82+
f *= 0.1;
8383
break;
8484
}
8585
}
@@ -194,11 +194,11 @@ boolean DHT::read(bool force) {
194194
_lastresult = false;
195195
return _lastresult;
196196
}
197-
data[i/8] <<= 1;
197+
data[i * 0.125] <<= 1;
198198
// Now compare the low and high cycle times to see if the bit is a 0 or 1.
199199
if (highCycles > lowCycles) {
200200
// High cycles are greater than 50us low cycle count, must be a 1.
201-
data[i/8] |= 1;
201+
data[i * 0.125] |= 1;
202202
}
203203
// Else high cycles are less than (or equal to, a weird case) the 50us low
204204
// cycle count so this must be a zero. Nothing needs to be changed in the

0 commit comments

Comments
 (0)