If the temperature, pressure, humidity, rainfall, and altitude structs were elements of an array of structs, you could iterate over those elements setting each one's high and low values with only two handwritten 'if-then' statements. If you also used an enum to name the indices of the structs, then in other parts of your code you could address each of them by a descriptive name:
// Declare a data struct and define an array of them.
struct {
int16_t current;
int16_t high;
int16_t low;
} data[5] = {0, 0, 0, 0, 0};
// Name the structs by index
enum kind {
temperature = 0,
pressure,
humidity,
rainfall,
altitude
};
// Assign one kind of data:
data[temperature].current = getTermperature();
// Set maxes & mins for all of the kinds of data:
for( i = 0; i < sizeof(data)/sizeof(data[0])); ++i ){
if ( data[i].current > data.high) {
data[i].high = data[i].current;
}
if ( data[i].current < data[i].low) {
data[i].low = data[i].current;
}
}
Update:
I'm strugling with the data[5] {0,0...} part in struct.
I can't see your whole sketch but you probably have allocated 5 separate structs named "temp", "pressure", ... , etc. I'm suggesting you allocate an array of 5 structs (which I collectively called "data"), one for each of your individual ones. I included an initializer ( '= {0,0,0,0,0}') to set everything to zero before the start of the run.
all of enum kind{} and how it becomes data[pressure] etc.,
The enum is a quick way to make 5 symbols that stand for consecutive integers, in this case, the index into the array of each struct, using a descriptive name for that struct's data. So 'data[pressure]' is the same as data[1] (in this case) but is more understandable.
the sizeof(data)/sizeof(data[0]) in the loop
That expression gives the number of structs in the array (the total byte-size of the array divided by the byte-size of one struct). We could write '5' instead, but when you someday add another member or two (wind_direction, wind_speed, perhaps?), you'd have to remember to also fix your for() loop. By using this calculation, the compiler will do it for you, leaving you one less opportunity to make a mistake you'd then need to debug.
and finally I think
altitude.high in the if statement is a paste typo? should be
data[i].high?
Absolutely right! Fixed now.
Update:
For defining initial values can you just assign an initial value in
the struct such as int16_t low = INFINITY; and so on instead of
{0,0,0,0,0}?
Yes - Without knowing your data and application, I chose the simplest initializer. The full syntax to do what you want would look like this:
// Declare a data struct and define an array of them.
struct {
int16_t current;
int16_t high;
int16_t low;
} data[5] = {
{0, 0, INFINITY},
{0, 0, INFINITY},
{0, 0, INFINITY},
{0, 0, INFINITY},
{0, 0, INFINITY}
};
You don't need (and may not) specify each data type and name; the struct declaration already provides that information. The initializers are positional and each one must be compatible with the data-type it matches up to.
ifstatements execute quickly and I doubt you are calling the function quickly enough for execution time to matter. Alternatively you could consider storing the data in an array rather than each variable separately - could make export easier. – MichaelT Jan 23 '19 at 21:22checkRecord()block should be indented ..... it is only a readability issue ..... you could single-line all theifstatements ....if ( temp.current > temp.high) temp.high = temp.current;..... then you could neatly line up everything in a column .... put all the equal signs under each other – jsotola Jan 24 '19 at 03:25