user53280: N00b question,
Is there a better way for readability / elegance / DRY etc to check various sensor values for high/low record value?  This code works, but looks like the long way round.
  void checkRecord() {

if ( temp.current > temp.high) {
  temp.high = temp.current;
}

if ( temp.current < temp.low) {
  temp.low = temp.current;
}

if ( pressure.current > pressure.high) {
  pressure.high = pressure.current;
}

if ( pressure.current < pressure.low) {
  pressure.low = pressure.current;
}
if ( humidity.current > humidity.high) {
  humidity.high = humidity.current;
}

if ( humidity.current < humidity.low) {
  humidity.low = humidity.current;
}

if ( rainfall.current > rainfall.high) {
  rainfall.high = rainfall.current;
}

if ( rainfall.current < rainfall.low) {
  rainfall.low = rainfall.current;
}
if ( altitude.current > altitude.high) {
  altitude.high = altitude.current;
}

if ( altitude.current < altitude.low) {
  altitude.low = altitude.current;
}
}

Thanks!

user50469: You could write something like:
temp.high = (temp.current > temp.high) ? temp.current : temp.high;
temp.low  = (temp.current < temp.low) ? temp.current : temp.low;
...

Most of the time there are min/max macros defined already like:
#define MAX(a, b)   ((a > b) ? a : b)
#define MIN(a, b)   ((a < b) ? a : b)

So you can then simply write:
temp.high = MAX(temp.current, temp.high);
temp.low  = MIN(temp.current, temp.low);
...

Which expands to the exact code as given above.

user7508: This is a variation on the struct idea with an object-oriented flavor.
If you put the current, low and high values of any sensor inside a
struct, you could also morph this struct into a class and give it
an update() method that, in addition to updating the current value,
keeps the low and high updated:
class LowHigh
{
public:
    LowHigh() : current(0), low(INFINITY), high(-INFINITY) {}
    void update(float value) {
        current = value;
        if (current < low)
            low = current;
        if (current > high)
            high = current;
    }
    float get_low() { return low; }
    float get_high() { return high; }
private:
    float current, low, high;
};

I am assuming float data here, you may have to adapt to whatever type
is appropriate for your sensors.
You would use the class as this:
LowHigh temp, pressure, humidity, rainfall, altitude;

void loop()
{
    temp.update(temp_sensor.get_reading());
    pressure.update(pressure_sensor.get_reading());
    humidity.update(humidity_sensor.get_reading());
    // ...
}


53280: <p>N00b question,</p>

<p>Is there a better way for readability / elegance / DRY etc to check various sensor values for high/low record value?  This code works, but looks like the long way round.</p>

<pre><code>  void checkRecord() {

if ( temp.current &gt; temp.high) {
  temp.high = temp.current;
}

if ( temp.current &lt; temp.low) {
  temp.low = temp.current;
}

if ( pressure.current &gt; pressure.high) {
  pressure.high = pressure.current;
}

if ( pressure.current &lt; pressure.low) {
  pressure.low = pressure.current;
}
if ( humidity.current &gt; humidity.high) {
  humidity.high = humidity.current;
}

if ( humidity.current &lt; humidity.low) {
  humidity.low = humidity.current;
}

if ( rainfall.current &gt; rainfall.high) {
  rainfall.high = rainfall.current;
}

if ( rainfall.current &lt; rainfall.low) {
  rainfall.low = rainfall.current;
}
if ( altitude.current &gt; altitude.high) {
  altitude.high = altitude.current;
}

if ( altitude.current &lt; altitude.low) {
  altitude.low = altitude.current;
}
}
</code></pre>

<p>Thanks!</p>
 50469: <p>You could write something like:</p>

<pre><code>temp.high = (temp.current &gt; temp.high) ? temp.current : temp.high;
temp.low  = (temp.current &lt; temp.low) ? temp.current : temp.low;
...
</code></pre>

<p>Most of the time there are min/max macros defined already like:</p>

<pre><code>#define MAX(a, b)   ((a &gt; b) ? a : b)
#define MIN(a, b)   ((a &lt; b) ? a : b)
</code></pre>

<p>So you can then simply write:</p>

<pre><code>temp.high = MAX(temp.current, temp.high);
temp.low  = MIN(temp.current, temp.low);
...
</code></pre>

<p>Which expands to the exact code as given above.</p>
 7508: <p>This is a variation on the <code>struct</code> idea with an object-oriented flavor.</p>

<p>If you put the current, low and high values of any sensor inside a
<code>struct</code>, you could also morph this <code>struct</code> into a <code>class</code> and give it
an <code>update()</code> method that, in addition to updating the current value,
keeps the low and high updated:</p>



<pre class="lang-c++ prettyprint-override"><code>class LowHigh
{
public:
    LowHigh() : current(0), low(INFINITY), high(-INFINITY) {}
    void update(float value) {
        current = value;
        if (current &lt; low)
            low = current;
        if (current &gt; high)
            high = current;
    }
    float get_low() { return low; }
    float get_high() { return high; }
private:
    float current, low, high;
};
</code></pre>

<p>I am assuming <code>float</code> data here, you may have to adapt to whatever type
is appropriate for your sensors.</p>

<p>You would use the class as this:</p>

<pre class="lang-c++ prettyprint-override"><code>LowHigh temp, pressure, humidity, rainfall, altitude;

void loop()
{
    temp.update(temp_sensor.get_reading());
    pressure.update(pressure_sensor.get_reading());
    humidity.update(humidity_sensor.get_reading());
    // ...
}
</code></pre>
