# Putting tmp36 temperature reads into an array

Hi, i am trying to put individual temperature float values into an array and then retrieve the highest temperature. Unfortunately my code is not giving me the correct results and i have researched this topic with no answers found. i first tried to put the values in an array and get the average and that resulted in the wrong average being printed to serial monitor with a y in front of the number
My code is below any help would be appreciated.

``````  /*******************************************/
float temp = ((analogRead (pinTemp)* (5.0/1024))-0.5)/0.01;
float tempArray; // temp array of 4 to test
int numTemp = 5;
float avg;
int i;
float sumOfTemp=0.0;

//for loops for processing data
Serial.println(temp);
for (i=0;i<numTemp;i=i+1){

tempArray[i]=temp;
}

for(i=0; i<numTemp;i=i+1){
sumOfTemp = sumOfTemp+tempArray[i];
}

avg = sumOfTemp/tempArray[i];
Serial.print("Average temperature is:");
Serial.println(avg);
Serial.println("");
``````

You’re confused about array size. tempArray makes an array of 4 elements, 0,1,2,3.

Yet you declare numTemp = 5 and later iterate with it, so 0,1,2,3,4.

This overflows the array.

``````float tempArray; // temp array of 4 to test
int numTemp = 5;
...
for (i=0;i<numTemp;i=i+1){
``````

The use of size constants is bass ackwards anyway, you should declare the size first and then use the reference:

``````int numTemp = 5;
float tempArray[numTemp]; // temp array of 5 to test
``````

Hi,

I have changed this and unfortunately it did not fix my problem

@jessie2020

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

jessie2020:
Hi,

I have changed this and unfortunately it did not fix my problem

You didn’t post your corrected code.
You didn’t post all your original code.

You average should be:

``````avg = sumOfTemp/numTemp;
``````

not:

``````avg = sumOfTemp/tempArray;
``````

Hi

This is what i changed:

``````//variables for processing data
int numTemp = 5;
float tempArray[numTemp]; // temp array
float avg=0.0;
int i;
float sumOfTemp= 0.0;

/*******************************************/
bool personClose= true;
//for loops for processing data
Serial.println(temp);

if (personClose){
for (i=0; i<numTemp; i=i+1){

tempArray[i]=temp;
}

}  for(i=1; i<=numTemp; i=i+1){
sumOfTemp = sumOfTemp+tempArray[i];
}

for( i= 0; I<tempArray; i++){
Serial.print(tempArray[i]); // to print out each element of the array
}
/******************************************/

avg = sumOfTemp/tempArray;
Serial.print("Average temperature is:");
Serial.println(avg);
Serial.println("");

Serial.print("Total temperature is:");
Serial.println(sumOfTemp);
Serial.println("");
``````

unfortunately it is not work for Reading the TMP36 sensor and putting the values in an array. The code is correct to convert the readings into celsius because serial print displays the correct number. The code for the conversion is:

``````float temp = ((analogRead(pinTemp)* (5.0/1024))-0.5)/0.01;
``````

p.s thanks

We need to see a complete, updated sketch. Not snippets.

Hi,

code is as follows:

``````#include <LiquidCrystal.h> // liquid crystal library
#define pinTemp A0

LiquidCrystal lcd (12,11,5,4,3,2);

void setup(){
Serial.begin(9600); // set baud rate
pinMode(pinTemp, INPUT);

lcd.begin(16,2);
lcd.setCursor(4,0);
lcd.setCursor(2,1);
lcd.print("Temperature..");
}

void loop(){

float underOver; // variable to hold degree plus or minus

Serial.println(temp);

//variables for processing data

int numTemp = 5;
float tempArray[numTemp]; // temp array
float avg=0.0;
int i;
int index= 0;
float sumOfTemp= 0.0;
bool personClose = false;

for (i=0; i<numTemp; i++){
tempArray[i]=temp;
index++;

}
if (numTemp ==5){
for (i=0; i < numTemp; i++){
Serial.print("Items in the array are: ");
Serial.println(tempArray[i]);
}
}
``````

Oh, sorry, one more thing. We don’t know what is wrong currently. Is it still that the average doesn’t seem correct? Can you explain exactly how you reached that conclusion?

By the way, you still are missing a closing bracket in loop()…

What does it print out when it runs? Can you post a text capture?

i am trying to put individual temperature float values into an array

then why do you fill all the elements with the same value?

``````  for (i = 0; i < numTemp; i++) {
tempArray[i] = temp;
index++;
``````

After that loop, you are guaranteed to have identical values in every element.

Also,

``````  if (numTemp == 5) {
``````

is futile, since you set it to 5 and never changed it anywhere.

Hi,

My aim is to put the values of temperature into an array then output the values and find the highest temperature in the array. Now it doesnt seem to be adding the temperature to the array as it is only displaying one value when i serial print array. Any suggestions how to properly populate the array with the float values? i have tried to no avail

That is because you are programming using the "shotgun" method - point the shotgun at the barn and hope that some of the pellets hit.

You need to analyze your requirements, and think it through step by step. Nobody else can do that for you.

If you're baffled, you need to do more research and possibly try a lot of simpler projects first so you know what you're doing.

For example, you apparently haven't decided over what time period the 5 samples should be captured. Temperature doesn't change by degrees in microseconds, not in a normally working sensor anyway. So even if you made the sketch work logically, it would still print the same 5 values if they were all captured in less than a millisecond.

As i am using a simulator. I wanted the temperature to be captured each time i change the dial to indicate the temperature has been changed

jessie2020:
As i am using a simulator. I wanted the temperature to be captured each time i change the dial to indicate the temperature has been changed

1. loop() does not have a closing brace

2. You are placing the same sample in every element of your tempArray

``````  for (i = 0; i < numTemp; i++) {
tempArray[i] = temp;
index++;

}
``````
1. numTemp was not modified
``````  if (numTemp == 5) {
for (i = 0; i < numTemp; i++) {
Serial.print("Items in the array are: ");
Serial.println(tempArray[i]);
}
}
``````
1. numTemp should be a const

2. You need a global or static variable for the array index in order to populate the array.

Here is the proper way to populate the array. You can adjust the sample interval to your needs. With simple modification you can convert to a running average.

``````const byte pinTemp = A0;
const unsigned long sampleInterval = 1000; // milliseconds
const int numTemp = 5;
float tempArray[numTemp]; // temperature array
unsigned long prevSampleMillis; // millis() that previous sample was read

void setup()
{
Serial.begin(9600); // set baud rate
pinMode(pinTemp, INPUT);
prevSampleMillis = millis();
}

void loop()
{
static int tempIndex = 0;

// Sample temperature if it is time to do so
if (millis() - prevSampleMillis >= sampleInterval)
{
prevSampleMillis = millis();
tempArray[tempIndex++] = ((analogRead(pinTemp) * (5.0 / 1024)) - 0.5) / 0.01;
if (tempIndex == numTemp)
{
// Array is full.  Print values.
tempIndex = 0;
float tempSum = 0;
Serial.print("Items in the array are: ");
for (int i = 0; i < numTemp; i++)
{
Serial.println(tempArray[i]);
tempSum += tempArray[i];
}
float tempAvg = tempSum/numTemp;
Serial.print("Temperature Average: ");
Serial.println(tempAvg);
}
}

// TBD: Do stuff here that needs to be done every loop
}
``````

jessie2020:
Hi,

My aim is to put the values of temperature into an array then output the values and find the highest temperature in the array. Now it doesnt seem to be adding the temperature to the array as it is only displaying one value when i serial print array. Any suggestions how to properly populate the array with the float values? i have tried to no avail

You didn’t try. There is nothing in your code that compares previous to current values, in order to cause the new value to be added to the array. Perhaps you should have a go at that.

As i am using a simulator. I wanted the temperature to be captured each time i change the dial to indicate the temperature has been changed

Also, don’t you think this is an important thing to know, for someone that is trying to help you? This should have been explained in the first post.