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(){

 int reading = analogRead(sensorPin);  
 

 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; 

const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
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:
    for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
}

void loop() {
    //getting the voltage reading from the temperature sensor
    int reading = analogRead(sensorPin);  
 
    // 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");
 
    // subtract the last reading:
    total = total - readings[readIndex];
     // read from the sensor:
    readings[readIndex] = analogRead(inputPin);
    // add the reading to the total:
    total = total + readings[readIndex];
    // advance to the next position in the array:
    readIndex = readIndex + 1;

    // if we're at the end of the array...
    if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
     }

    // calculate the average:
    average = total / numReadings;
    // 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

As to your problem, your code looks way too complicated

Some pseudo code for you

set total to zero
set reading count to zero

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

  if reading count equals 10
    do whatever you want with the total
    set total to zero
    set reading count to zero
   end if

//any other non blocking code you want goes here
end of loop()

I hope that you are not using delay() in loop() as this will prevent the free running of the code

Thank you that was very helpful, I didn't know you had to set the total and reading count to 0 afterwards also, I thought you only had to do it before. I will make sure to not use delay