         break;  // do nothing 
     }
   }
-  delay(1000); // 1Hz rep of main loop
+  // Poll rain sensor and determine average and standard deviation
+  i = 0;
+  rain_mv_sum = 0;
+  rain_mv_sumsqr = 0;
+  while(i<NUMPNTS) {
+    // read rain sensor analog input
+    rain_mv = MILLIVOLTS_PER_COUNT * float(analogRead(APIN_RAIN));
+    // sum the values
+    rain_mv_sum += rain_mv;
+    rain_mv_sumsqr += rain_mv*rain_mv;
+    i+=1;
+    delay(1000/NUMPNTS); // Polling the rain sensor every 1000ms / N
+  } // repeat loop for 1 second user interaction time
+  // calculate avg and standard deviation
+  rain_mv_avg = rain_mv_sum / float(NUMPNTS);
+  rain_mv_std = sqrt((1/float(NUMPNTS-1)) * (rain_mv_sumsqr - float(NUMPNTS)*rain_mv_avg*rain_mv_avg));
+  if (verbose_logging) {
+    Serial.print("Rainsensor avg = ");
+    Serial.print(rain_mv_avg);
+    Serial.print(" std = ");
+    Serial.print(rain_mv_std);
+    Serial.println(" mV");
+  }
+  if ((rain_mv_avg+2*rain_mv_std) < RAIN_THRESHOLD_MV) {
+    condition = COND_RAIN;
+  }
+  else {
+    condition = COND_CLEAR;
+  }
 }
 
 void report_state() {

// f = Open cover and try to keep it open as much as possible
// c = Close cover
// ? = Report cover state (open/closed & rain)
// 
// Event feedback:
// If the cover closes or opens, that is sent as "closing by request" or "closing by rain"

void setup() {
  // Start serial for input / output
  Serial.begin(9600);
  Serial.println("EM27Sun Cover Driver v1.3");
  Serial.print("Rain threshold set at ");
  Serial.print(RAIN_THRESHOLD_MV);
  Serial.println("mV");
  // Set up for the Stepper motor
  stepper.setPinsInverted(true,false,true); // sets direction and enable pins inverted
  stepper.setMaxSpeed(USTEP_DIVISOR * STEP_SPEED);
