Averaging sensor values and showing only the final Averaged value

Hello. I have been having a hard time finding out how to do this.
I have a DS18B20 temperature sensor and I want it to work the same as our pharmacy based digital thermometer.

Digital Thermometer bought from pharmacies (I think) loops and averages sensor readings then only displays the final averaged reading.

I can’t seem to make that work in my project using DS18B20. It just loops everything and when compared to the digital thermometer, when I placed both sensors at my armpit at the same time, DS18B20 is still at 36.05 degrees C while the digital thermometer reached its peak and stopped at 36.4 degrees C.

Any help will be so much appreciated. Thank you.

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
int x = 0;
int i = 0;

const int numReadings = 10;

float readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
float total = 0;                  // the running total
float average = 0;

float temperature;

void setup() {
  DS18B20.begin();
  Serial.begin(9600);
    for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {// subtract the last reading:
  total = total - readings[readIndex];
  getTemperature();
  readings[readIndex] = temperature ;
  total = total + readings[readIndex];
  readIndex = readIndex + 1;

  if (readIndex >= numReadings) {
    Serial.println("stop");
  }

  average = total / numReadings;
  Serial.println(average);
  delay(1);        // delay in between reads for stability
}


void getTemperature() {
  do {
    DS18B20.requestTemperatures();
    temperature = DS18B20.getTempCByIndex(0);
  } while (temperature == 85.0 || temperature == (-127.0));
}

Please, place both sensors (the thermometer and DS18B20) in a cup filled up with normal water. Record the temperatures and report in this post. We need to investigate the causes of the difference between the readings of these two sensors. Also please, give a link to your thermometer.

It looks like you are taking a fixed number of readings, seems to me you should be taking readings until they stop changing.

Also the difference between the two sensors id not much so you may have an issue with calibration or the accuracy of your sensor as noted above by the request for a link to your sensor - people tend to take sensor readings as absolute as opposed to the +/- that all sensors have

Is the DS18B20 insulated in a metal jacket and potting compound?
I have never seen a data sheet showing how long it takes to clear all errors.
My jacketed units are slow to respond and slow to stabilize

It just loops everything

Your main loop increases "readIndex" without bounds. As soon as readIndex is greater than 9, you start overwriting memory that you don't own, and the program crashes.

The damage starts after you print "stop". It would be a reasonable idea to reset readIndex to 0 at this point.

  if (readIndex >= numReadings) {
    Serial.println("stop");
  }

Interestingly, the value of readIndex variable does not exceed 11.
sm39x.png

sm39x.png

Already 10 is too much… 11..... Code is out in space. The array is declared to hold 10 units, adressed from index 0 to index 9. accessing readings[10] is a memory violation already.

Railroader:
Already 10 is too much… 11..... Code is out in space. The array is declared to hold 10 units, adressed from index 0 to index 9. accessing readings[10] is a memory violation already.

Is it out-of-bound and could be a potential agent for program/system crash?

Is it out-of-bound and could be a potential agent for program/system crash?

Since you seem to have doubts about whether writing to out of bounds memory locations has consequences, can you explain the behavior of the program you posted in reply #5?

Writing to out of bounds memory is never good - but I did have an instructor that it did not seem to bother him - but when you write out of bounds - if that piece of memory is used by another part of the program you can get erratic operation or the program can crash because the wrong value is used

I finally standardized on using the zero element of an array to hold the index number for the the array then 1 to n for the array index - so holding 10 values in an array would be an array of 11 elements - with the fort or zero element for the index value and values in elements 1 to 10

that also helped me to avoid the off by one problem getting the correct element of an array - was easier for me to think of the elements

hope I did not confuse things too much

Declare the array as ….[11] then...

I have not yet been able to figure out how the variable readIndex can grow without bounds as has been predicted in Post#4. Experiment says that the variable grows upto 11 and then begins from 1.

The problem is that you initialize the array as follows:

float readings[numReadings];

where numReadings is 10. That means that there are ten addressable elements (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9).

If you write to readings[10] or readings[11] you're outside the array and writing over who knows what.

Your maximum readIndex value must be "numReadings-1".