Remove the firstreading variable

By cleverly setting _lastreadtime in begin() to make sure that it looks
like the last read was alrady 2000 ms ago, even on startup when millis()
still returns 0, there is no longer a need to keep a separate
firstreading variable, saving a byte of memory and a bit of code.
This commit is contained in:
Matthijs Kooijman 2015-07-24 21:17:16 +02:00
parent f1b79028ea
commit 5973929e63
2 changed files with 7 additions and 5 deletions

11
DHT.cpp
View File

@ -6,10 +6,11 @@ written by Adafruit Industries
#include "DHT.h" #include "DHT.h"
#define MIN_INTERVAL 2000
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) { DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
_pin = pin; _pin = pin;
_type = type; _type = type;
_firstreading = true;
_bit = digitalPinToBitMask(pin); _bit = digitalPinToBitMask(pin);
_port = digitalPinToPort(pin); _port = digitalPinToPort(pin);
_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for _maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for
@ -22,7 +23,10 @@ void DHT::begin(void) {
// set up the pins! // set up the pins!
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
digitalWrite(_pin, HIGH); digitalWrite(_pin, HIGH);
_lastreadtime = 0; // Using this value makes sure that millis() - lastreadtime will be
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
// but so will the subtraction.
_lastreadtime = -MIN_INTERVAL;
DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC); DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
} }
@ -117,10 +121,9 @@ boolean DHT::read(bool force) {
// Check if sensor was read less than two seconds ago and return early // Check if sensor was read less than two seconds ago and return early
// to use last reading. // to use last reading.
uint32_t currenttime = millis(); uint32_t currenttime = millis();
if (!force && !_firstreading && ((currenttime - _lastreadtime) < 2000)) { if (!force && ((currenttime - _lastreadtime) < 2000)) {
return _lastresult; // return last correct measurement return _lastresult; // return last correct measurement
} }
_firstreading = false;
_lastreadtime = currenttime; _lastreadtime = currenttime;
// Reset 40 bits of received data to zero. // Reset 40 bits of received data to zero.

1
DHT.h
View File

@ -50,7 +50,6 @@ class DHT {
uint8_t data[6]; uint8_t data[6];
uint8_t _pin, _type, _bit, _port; uint8_t _pin, _type, _bit, _port;
uint32_t _lastreadtime, _maxcycles; uint32_t _lastreadtime, _maxcycles;
bool _firstreading;
bool _lastresult; bool _lastresult;
uint32_t expectPulse(bool level); uint32_t expectPulse(bool level);