Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare time differences instead of timestamps #1648

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions main/ZsensorADC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
ADC_MODE(ADC_TOUT);
# endif

static int persistedadc = -1024; // so that the first reading will always be published

//Time used to wait for an interval before resending adc value
unsigned long timeadc = 0;
unsigned long timeadcpub = 0;
Expand All @@ -42,13 +44,12 @@ void setupADC() {
}

void MeasureADC() {
if (millis() > (timeadc + TimeBetweenReadingADC)) { //retrieving value of temperature and humidity of the box from DHT every xUL
if (millis() - timeadc > TimeBetweenReadingADC) { //retrieving value of temperature and humidity of the box from DHT every xUL
# if defined(ESP8266)
yield();
analogRead(ADC_GPIO); //the reliability of the readings can be significantly improved by discarding an initial reading and then averaging the results
# endif
timeadc = millis();
static int persistedadc;
int val = analogRead(ADC_GPIO);
int sum_val = val;
if (NumberOfReadingsADC > 1) { // add extra measurement for accuracy
Expand All @@ -60,7 +61,7 @@ void MeasureADC() {
if (isnan(val)) {
Log.error(F("Failed to read from ADC !" CR));
} else {
if (val >= persistedadc + ThresholdReadingADC || val <= persistedadc - ThresholdReadingADC || (MinTimeInSecBetweenPublishingADC > 0 && millis() > (timeadcpub + (MinTimeInSecBetweenPublishingADC * 1000UL)))) {
if (val >= persistedadc + ThresholdReadingADC || val <= persistedadc - ThresholdReadingADC || (MinTimeInSecBetweenPublishingADC > 0 && millis() - timeadcpub > (MinTimeInSecBetweenPublishingADC * 1000UL))) {
timeadcpub = millis();
Log.trace(F("Creating ADC buffer" CR));
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
Expand Down