   returnValue.temp = DHT.temperature;
   returnValue.humidity = DHT.humidity;
   return returnValue;
+}
+
+
+// An utterly naive bang-bang controller with a timeout to prevent too-frequent activations 
+void updateState (ControlledValue &variable)
+{
+  switch (variable.state) {
+    case NOT_CONTROLLING:
+      if (_runtimeInSeconds > variable.lastStateChangeTime+MAX_SWITCH_RATE) {
+        if (variable.current > variable.target + variable.tolerance) {
+          variable.lastStateChangeTime = _runtimeInSeconds;
+          variable.state = CONTROLLING_DOWN;
+          digitalWrite (variable.pinToDecrease, HIGH);
+        } else if (variable.current < variable.target - variable.tolerance) {
+          variable.lastStateChangeTime = _runtimeInSeconds;
+          variable.state = CONTROLLING_UP;
+          digitalWrite (variable.pinToIncrease, HIGH);
+        }
+      }
+      break;
+    case CONTROLLING_UP:
+      if (variable.current > variable.target) {
+        variable.lastStateChangeTime = _runtimeInSeconds;
+        variable.state = NOT_CONTROLLING;
+        digitalWrite (variable.pinToIncrease, LOW);
+      }
+      break;
+    case CONTROLLING_DOWN:
+      if (variable.current < variable.target) {
+        variable.lastStateChangeTime = _runtimeInSeconds;
+        variable.state = NOT_CONTROLLING;
+        digitalWrite (variable.pinToDecrease, LOW);
+      }
+      break;
+  }
 }
 
 

  _averageHumidityLastMonth = 0;
  _averageCoolingOvershoot = 0.0;
    
  pinMode(COOLING_PIN, OUTPUT);
  digitalWrite (COOLING_PIN, LOW);
    
  pinMode(DEHUMIDIFIER_PIN, OUTPUT);
  digitalWrite (DEHUMIDIFIER_PIN, LOW);
  
  Serial.begin(115200);
  Serial.println ("\n\tDay\tHour\tMin\tSec\tT\tRH\tCool\tDehum.\tRunning Averages");

  uint32_t timeSinceLastHumidityActuation;
  uint32_t currentTime = millis();
  if (currentTime < _lastCheckTime) {
    // Happens once every 50 days or so
    _numWraparounds++;
  } 
  _lastCheckTime = currentTime;
  _runtimeInSeconds = _numWraparounds * (UINT32_MAX/1000) + (currentTime/1000);
  const uint32_t pollRate = 15000; // Defaults to every fifteen seconds in the normal case
