Hello everyone! im fairly new to coding and was wondering how i can create (or how to start) a program that compiles 5 average temperature readings taken every second and display that final average temperature on the serial console using the MCP9808 sensor.
I already have the board and component wired up and a library already in use. Was just wondering how i would program the sensor to take the average of 5 temperature readings and display the final temperature in C.
int sum = analogRead(analoPin);
sum += analogRead(analoPin);
sum += analogRead(analoPin);
sum += analogRead(analoPin);
sum += analogRead(analoPin);
analogAverageValue = sum / 5;
First please fix your post above. You should always use code tags when posting code. The forum guide explains how. Without the tags, the forum software can mess up the code you post, making it difficult to read or making it look like you made mistakes that are not in your actual code.
Also please format your code before you post it again by clicking Tools->Auto Format.
float c = tempsensor.readTempC();
c += tempsensor.readTempC();
c += tempsensor.readTempC();
c += tempsensor.readTempC();
c += tempsensor.readTempC();
c = c / 5.0;
Serial.print("Temp: ");
Do you mean 5 readings in 1 second or 5 readings in 5 seconds?
Either way, you can use the code @Railroader suggested but with a delay() between each reading. The length of the delay depends on the question above: either delay(200) or delay (1000).
thank you for the advice! and yes so essentially the goal is for the sensor to take 5 readings in 1 second and then give out the average of those 5 within that 1 second
I'm not sure that its taking the sum and averaging the 5 readings. It's still only giving me readings one by one even when I add delays() to meet the parameters of the program (5 readings in 1 second turned to 1 final average after that second)
Look at page 3! There it tells how many conversions the unit makes per second. Using delay to make sure a new conversion is done, it ought to work. Try and make a Serial.print after each reading.
sum = sensor.read();
delay(???);
Serial.println(sum);
tmp a = sensor.read();
delay(???);
Serial.println(tmp);
Sum += tmp;
tmp a = sensor.read();
delay(???);
Serial.println(tmp);
Sum += tmp;
tmp a = sensor.read();
delay(???);
Serial.println(tmp);
Sum += tmp;
tmp a = sensor.read();
Serial.println(tmp);
Sum += tmp;
Value = Sum / 5;
Why do you need to wake up the sensor again and again?
If you answer because you shut it down again and again (waiting on seeing your code!) why do you shut it down, ever? Just wake it up in your setup() function once.