   digitalWrite(doorBottom, HIGH); // Enable resistor
   digitalWrite(fan, LOW);
   digitalWrite(fanSpeed, LOW);
-
-}
-
-
-
+}
+
+/**
+ * Main program loop
+ */
 void loop() {
   esp.process();
+
+  // Only fetch new sensor data if it's been long enough since the last reading.
+  if ((unsigned long)(millis() - readingInterval) >= lastReading) {
+    // Read new data from sensors
+    readSensors();
+
+    // Respond ot sensor data
+    handleSensorReadings();
+  }
+
+  // Move the door as needed
+  doorMove();
+
+  // Update door LEDs as needed
+  doorLEDs();
   
-  if ((unsigned long)(millis() - readingInterval) >= lastReading) {
-    // Do stuff!
-    readSensors();
-    updateLCD();
-    // 
-  }
-}
+  // Update the LCD display
+  updateLCD();
+}

  esp.reset();
  delay(500);
  while(!esp.ready());
  
  if (Debugging) {
    debugger.println("ARDUINO: Setup MQTT client");
  }
  lcdWrite("Initializing...", "MQTT Connecting");
  if (!mqtt.begin("coop_duino", "", "", 120, 1)) { // client_id, username, password, keepalive time, cleansession boolean
    if (Debugging) {
      debugger.println("ARDUINO: Failed to setup MQTT");
    }
    lcdWrite("MQTT Connect", "Failure");
    while(1);
  }
  
  mqtt.connectedCb.attach(&mqttConnected);
  mqtt.disconnectedCb.attach(&mqttDisconnected);
  mqtt.publishedCb.attach(&mqttPublished);
  mqtt.dataCb.attach(&mqttData);
  
  if (Debugging) {
    debugger.println("ARDUINO: Setup WIFI");
  }
  esp.wifiCb.attach(&wifiCb);
  esp.wifiConnect(wHost, wPass);
  
  lcdWrite("System Ready");
  
  /**
   * Setup pin modes
   */
  pinMode(lampRelay, OUTPUT);
  pinMode(heaterRelay, OUTPUT);  
  pinMode(doorOpen, OUTPUT);
  pinMode(doorClose, OUTPUT);
  pinMode(doorSpeed, OUTPUT);

 * \author
 *       Will Vincent <will@willvincent.com>
 */

#include <Wire.h>
#include <OneWire.h>
#include <LiquidCrystal.h>
#include <espduino.h>
#include <mqtt.h>
#include "RTClib.h"

// print debug messages or not to serial
const boolean Debugging = true;

