Avarage temperature problem

Hi, sorry if im putting this in the wrong category, i am very new, so i am a computer science student and i need to make code to take the temperature and display it in TMP:##.# format, and i also need it to calculate the average temp in its lifetime (TMP_AVG:##.#) format, and with up button i need to reset the avarage using the debounce command, i am using 16.02 and i have the temp sensor connected.

Thank you.

What have you tried so far?

my friend gave me this code, but it needs some modifications to make it so that i only need it to count the current temp. and the avarage as said in original post:

#include <LiquidCrystal.h>
#include <math.h>
const char *button[5] ={"Right ", "Up ", "Down ", "Left ", "Unknown "};
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

unsigned long prevTime = 0;
unsigned long debounceDelay = 200;
unsigned long lastDebounceTime = 0;
int limh = 0;
int liml = 0;
double temp = 0;

//Buttons
#define btnRight 0
#define btnUp 1
#define btnDown 2
#define btnLeft 3
#define btnUnknown 4

//Button readings
int buttons(){
int buttonread = analogRead(0);
int ret = btnUnknown;

if(buttonread < 10) ret = btnRight;
if((buttonread > 140) && (buttonread < 150)) ret = btnUp;
if((buttonread > 320) && (buttonread < 340)) ret = btnDown;
if((buttonread > 500) && (buttonread < 520)) ret = btnLeft;
return ret;

}

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print(“Thermistor”);

delay(1000);
lcd.clear();
}

void loop() {

int analogvalue = analogRead(A1);
//Steinhart-Hart equation
if((millis() - prevTime) >=2000) {
temp = log(10000.0/((1024.0/analogvalue-1)));
temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp ))* temp );
temp = temp - 273.15; // Kelvin to celcius

prevTime = millis();

//Round temp
//int roundTemp = round(temp);

}

if((millis() - lastDebounceTime) > debounceDelay) {
//LIMH
if(buttons() == btnUp) {
lcd.clear();
limh++;
}
if(buttons() == btnDown) {
lcd.clear();
limh–;
}
//LIML
if(buttons() == btnLeft) {
lcd.clear();
liml++;
}
if(buttons() == btnRight) {
lcd.clear();
liml–;
}

lastDebounceTime = millis();
}

if(temp < limh && temp > liml) {

lcd.setCursor(12,1);
lcd.print(“NORM”);
}
if(temp > limh) {
lcd.setCursor(12,1);
lcd.print(“HIGH”);
}
if(temp < liml) {

lcd.setCursor(12,1);
lcd.print(“LOW”);
}
lcd.setCursor(0, 1);
lcd.print(“TMP:”);
lcd.setCursor(4, 1);
lcd.print(temp);
//Display on LCD
lcd.setCursor(0,0);
lcd.print(“LIMH:”);
lcd.setCursor(5,0);
lcd.print(limh);
lcd.setCursor(8,0);
lcd.print(“LIML:”);
lcd.setCursor(13,0);
lcd.print(liml);

That code is incomplete, and lacks code tags.

yeah sorry im new, really stuck on arduino and this code

i edited the code a little bit and removed some stuff i dont think i need, i still need it to show the avarage temp, and reset with up button intsead of reset button

#include <LiquidCrystal.h>
#include <math.h>
const char *button[5] ={"Right ", "Up ", "Down ", "Left ", "Unknown "};
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

unsigned long prevTime = 0;
unsigned long debounceDelay = 200;
unsigned long lastDebounceTime = 0;
int limh = 0;
int liml = 0;
double temp = 0;

//Buttons
#define btnRight 0
#define btnUp 1
#define btnDown 2
#define btnLeft 3
#define btnUnknown 4

//Button readings
int buttons(){
int buttonread = analogRead(0);
int ret = btnUp;

}

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print(“Thermistor”);

delay(1000);
lcd.clear();
}

void loop() {

int analogvalue = analogRead(A1);
if((millis() - prevTime) >=2000) {
temp = log(10000.0/((1024.0/analogvalue-1)));
temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp ))* temp );
temp = temp - 273.15; // Kelvin to celcius
prevTime = millis();

//Round temp
//int roundTemp = round(temp);

}

if((millis() - lastDebounceTime) > debounceDelay) {
if(buttons() == btnUp) {
lcd.clear();
}

lastDebounceTime = millis();
}
lcd.setCursor(0, 1);
lcd.print(“TMP:”);
lcd.setCursor(4, 1);
lcd.print(temp);
lcd.setCursor(0,0);
lcd.print(“TMP_AVG:”);

The code as posted doesn't compile so it doesn't do anything at all, though adding the final } would fix that.

Have you ever actually loaded and run it? If so what exactly does it do when you run it?

Steve

the original code was missing an "}", sorry, yes did try it, try the newer code that i commented, its much more clean and will compile like that

A computer science student should read directions, like "How to use this forum", and learn to use code tags.

ErkiH:
the original code was missing an "}", sorry, yes did try it, try the newer code that i commented, its much more clean and will compile like that

None of the code you posted will compile without the missing }.

But you still haven't said what your code actually does. To me it looks like a collection of random bits of code with no logic connecting them...but perhaps I'm just misreading it.

You have an array called 'button' which is then never used. You have a function called buttons() which only reads one pin but never returns anything anyway. Then there's something that compares the int that wasn't returned from buttons() to 1 then clears a display. I can't see any attempt at averaging anything. Averaging is usually easy, you just add things up and then divide by the number of things.

Steve