Hello everybody,
I have a thermistor to measure temperature.
I want to find the Max and Min value from one second reading.
So I found this topic Finding the max and min value of sensor reading - Sensors - Arduino Forum and try it.
But when I try to find the Max and the Min, it wont show me the Max and Min for every second. It showed me the Max and the Min from the whole sensor reading.
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
int numReadings = 40; //40 sensor values for one second reading
int readings[40];
int index = 0;
float sensorMax = 0;
float sensorMin = 1023;
void setup()
{
Serial.begin(9600);
// sensorMax = analogRead(A1);
// sensorMin = sensorMax;
}
void loop()
{
// const unsigned long tenscnds = 10000;
// static unsigned long lastSampleTime = 0 - tenscnds; // initialize such that a reading is due the first time through loop()
//
// unsigned long now = millis();
// if (now - lastSampleTime >= tenscnds)
// {
// lastSampleTime += tenscnds;
// //code disini
Vo = analogRead(A1);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
Tc = T - 273.15;
Serial.print("Temperature: ");
Serial.print(Tc);
Serial.println(" C");
const unsigned long onecnds = 1000;
static unsigned long lastSampleTime = 0 - onecnds; // initialize such that a reading is due the first time through loop()
unsigned long now = millis();
if (now - lastSampleTime >= onecnds)
{
lastSampleTime += onecnds;
// add code to take temperature reading here
// MEASUREMENTS
// every iteration of loop makes one additional measurement,
// so the first 10 readings will display too low average
float value = Tc;
readings[index] = value;
index++;
if (index >= numReadings) index = 0;
// DO SOME MATH
if (value > sensorMax) sensorMax = value;
if (value < sensorMin) sensorMin = value;
//running average
float total = 0;
for (int i = 0; i< numReadings; i++) total += readings[i];
float average = total /numReadings;
// OUTPUT TO SERIAL
Serial.print("\tMIN:\t");
Serial.print(sensorMin);
Serial.print("\tMAX:\t");
Serial.print(sensorMax);
Serial.print("\tAVG:\t");
Serial.println(average, 1); // 1 decimal
Serial.println();
}
//add code to do other stuff here
}
How can I find Max and Min among 40 sensor values for one second?
Thanks before.