# data logger and averaging temperatures

Hi guys,

I am new to this and working on a fairly large project for someone of my skill set (gotta love undergrad research). I am controlling a heating apparatus and need to use a data logger (I have the sparkfun qwiic openlog) to log temperatures and time as I go. Additionally, I need to average out the temperatures, every 10 readings so not a running average, and am not sure how to do this. I came across a few sample codes but I don’t know how to manipulate them to be able to do what i want. Any help would be greatly appreciated!

Here is just the part of my code for the temperature. would I add the averaging code right after this?

``````void loop(){

float voltage = reading * 5.0;
voltage /= 1024.0;

Serial.print(voltage); Serial.println(" volts");

float temperatureC = (voltage - 0.5) * 100 ;

lcd.print(temperatureC); lcd.println(" degrees C");

}
``````

EDIT i have attached my further code below, but I am still not sure if it’s right

I need to average out the temperatures, every 10 readings so not a running average, and am not sure how to do this. I came across a few sample codes but I don't know how to manipulate them to be able to do what i want.

Set the total to zero. Take 10 readings at the required interval and when you do so add each reading to the total. After 10 readings divide the total by 10. Repeat as often as you like.

Where are you stuck ?

UKHeliBob: Set the total to zero. Take 10 readings at the required interval and when you do so add each reading to the total. After 10 readings divide the total by 10. Repeat as often as you like.

Where are you stuck ?

I'm mainly stuck on how to write it out, do I use int or float? Also how do I repeat it? I figured it would do that on its own. Also I realized I can do a running average, but that doesn't help me much.

does this look right? sorry if the formatting is weird

``````int sensorPin = 0;

int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup() {
Serial.begin(9600);

// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
}

void loop() {
//getting the voltage reading from the temperature sensor

// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;

// print out the voltage
Serial.print(voltage); Serial.println(" volts");

// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
lcd.print(temperatureC); lcd.println(" degrees C");

// advance to the next position in the array:

// if we're at the end of the array...
// ...wrap around to the beginning:
}

// calculate the average:
// send it to the computer as ASCII digits
Serial.println(average);
delay(1);        // delay in between reads for stability

}
``````

Honestly, you need to take a programming course or something, because otherwise it will end up with us doing the project for you.

.

ieee488: Honestly, you need to take a programming course or something, because otherwise it will end up with us doing the project for you.

.

I already have over 500 lines of code for this project and this is one part I am stuck on. I am not asking for someone to do the whole thing for me, thank you very much.

I am new to this and trying to learn it on my own (since that is my only option), I didn't think asking for help or advice would be so bad, I thought that's what this forum was for.

sorry if the formatting is weird

Have you tried Auto Format in the IDE

Some pseudo code for you

``````set total to zero

start of loop()
if it is time for a new reading (use millis() timing to do this)
do whatever else you need to with the reading
end if