Temp code

This is my code for my temperature sensor. I want it to measure the air temperature, along with the max and min temperature readings. In my code, it can measure temperature, and keep the max temperature, but it can't keep the minimum temperature (it keeps displaying 0 degrees celsius for minimum temperature). Can someone please look at the code and help me out. I tried putting in other values for "float mini = " but it still didn't work out to give me the minimum temperature.

#define pin A0 // analog pin
float tempc = 0; // temperature variables
float val1;
float maxi = 0; // for max temperature
float mini = 0; //for min temperature

void setup()
{

Serial.begin(9600);
}
void loop()
{

val1 = ( 5.0 * analogRead(pin)) / 1024.0;
tempc = (20*val1) - 2.83;

delay(1000);
if(tempc > maxi)
{
maxi = tempc;
} // set max temperature
if(mini < maxi)
{
mini = tempc;
} // set min temperature
Serial.print("?f"); // clear the LCD
delay(1000); // wait to give the display time to clear

Serial.print("?x00?y0"); // write message on display line 0

Serial.print(tempc,2);
Serial.println(" Celsius, ");
//delay(10000);

Serial.print(maxi,2);
Serial.println(" Max, ");
Serial.print(mini,2);
Serial.println(" Min");

tempc = 0;

delay(10000); // delay before loop
}

First, don't set mini to 0 at the start... it can't get lower. Set it to a high value.
Then:
if(mini < maxi) Should be if(mini < tempc)

Sorry, i was trying different things, and i forgot to change it back to mini<tempc.

Thanks for your advice. How high a value should it be? i wanted the sensor to measure as low as 0 degrees, but if that is what's causing problems then i guess i should change it.

How high a value should it be?

You could set both minimum and maximum to a first initial reading, so something like this: (with some extra mods)

(code not tested)

#define TEMP_PIN A0    // analog pin

float tempC = 0.0; 
float maxTempC = 0.0;  // good var names explain themselves ;)
float minTempC = 0.0;   

// temperature fetch function
// note the condensed math
float getTempC()
{
  return (analogRead(TEMP_PIN)/ 10.24) - 2.83;
}



void setup()
{
  Serial.begin(9600);
  Serial.println("-- Start --");

  // INIT vars
  minTempC = maxTempC = tempC = getTempC();
}

void loop()
{
  // DO MEASUREMENT
  tempC = getTempC();

  // PROCESS MEASUREMENTS
  maxTempC = max(maxTempC , tempC );
  minTempC = min(minTempC , tempC );

  // DISPLAY OUTPUT
  Serial.print("?f");                  // clear the LCD
  delay(1000);                         // wait to give the display time to clear
    
  Serial.print("?x00?y0");             // write message on display line 0

  Serial.print(tempC , 2);
  Serial.println(" Celsius, ");

  Serial.print(maxTempC , 2);
  Serial.println(" Max, ");

  Serial.print(minTempC , 2);
  Serial.println(" Min");
}

Otherwise set mini to a higher value than the sensor can return, maxi to a lower value than the sensor can return. -99999 and 99999 would be fairly good choices as seeing them immediate warns a human that something is wrong (ie no samples have been taken yet).

Thanks for your help, everyone.

robtillart, your code worked for me. The simplifying helped a lot. Thank you so much!