I made this to readout values from a serial device.
As you can see it takes up to 20 measurements between a defined min/max.
I would like to add the possibility to find the highest and/or lowest value out of these measurements and save them to a new variable or overwrite the existing variable "Icol".
Unfortunately I need to use Serial.readString(); to get valid data
You don’t “need to”, you chose to…
The String class has a (not error proof) method to convert the String to a float
To find the min and max,define two float variables minVal and maxVal, and initialize minVal whith the largest possible expected value and maxVal with the lowest and each time you get a new value compare with minVal and maxVal and update appropriately
Set the minVal and maxVal out of range. If, for example, the room temperature is measured, then set the minVal to +1000.0 and the maxVal to -1000.0. It allows to detect if a new value was set or not.
Set the minVal and maxVal to the first value and try to find a lower and higher one. This is safer, but the first value has to be available at the moment of initialization.
With the "range based" for-loop, the code is very simple:
const float numbers[] =
{
10.0, 100.0, -123.0, 560.3, 3.5, 7.123456, 0.0, -10.5,
};
void setup()
{
Serial.begin( 115200);
// find min
float min = numbers[0];
for( auto a:numbers) // "range based" for-loop
if( a < min)
min = a;
// find max
float max = numbers[0];
for( auto a:numbers)
if( a > max)
max = a;
Serial.print( "Minimum = ");
Serial.println( min);
Serial.print( "Maximum = ");
Serial.println( max);
}
void loop()
{
}