+#include <algorithm>
+
 unsigned long restartPressed = 0;
 const short RESTART_DURATION = 5000;
 short threshold = 0;
 byte hasMailPrevious = 0;
 WiFiClient client;
+const byte MAX_SAMPLES = 21;
+short values[MAX_SAMPLES] = {0};
+byte _currentIndex = 0;
+
+byte currentIndex()
+{
+  return (_currentIndex = (_currentIndex + 1) % MAX_SAMPLES);
+}
+
+short medianValue()
+{
+  short copy[MAX_SAMPLES] = {0};
+  memcpy(copy, values, sizeof(values));
+  std::sort(std::begin(copy), std::end(copy));
+  return copy[MAX_SAMPLES / 2];
+}
 
 void sensorSetup() {
   pinMode(13, INPUT);
 
-  WiFi.mode(WIFI_STA);
+  WiFi.mode(WIFI_OFF);
+
   Serial.println("Calibrating...");
   short value;
-  for(int i = 0; i < 20; i++)
+  for(int i = 0; i < MAX_SAMPLES; i++)
   {
     value = system_adc_read();
-    if (value > threshold)
-    {
-      threshold = value;
-    }
-    delay(50);
+    Serial.print(value);
+    Serial.print(' ');
+    values[currentIndex()] = value;
+    delay(100);
   }
-  threshold += 5;
+  Serial.println();
+  threshold = medianValue() + 5;
   Serial.print("Threshold is ");
   Serial.print(threshold);
   Serial.println(".");


void sendEmail()
{
  long start = millis();
  if (client.connect("going-postal.me", 80)) {
    client.print("POST /api/test/email HTTP/1.1\r\n");


  }
  client.stop();
  WiFi.mode(WIFI_OFF);
  Serial.println(millis() - start);
}
