 //--> Arduino Main Loop <--
 
 void loop(void){
-  unsigned long currentMillis = millis();
-  
-  // Is it time to check the temp?
-  if(currentMillis - previousMillis > tempInterval) {
-    previousMillis = currentMillis;
-    
-    temp = checkTemp();
-    relayStatus = controlRelay(temp, desiredTemp);
-    
-    if (temp != oldTemp){
-      refreshDisplay = true;
-      oldTemp = temp;
-    }
-    else if (relayStatus != oldRelayStatus){
-      refreshDisplay = true;
-      oldRelayStatus = relayStatus;
-    }
-  }
-  
-  if (refreshDisplay == true){
-    printDisplay(displayMode);
-  }
-}
-
+//  unsigned long currentMillis = millis();
+//
+//  // Is it time to check the temp?
+//  if(currentMillis - previousMillis > tempInterval) {
+//    previousMillis = currentMillis;
+//
+//    temp = checkTemp();
+//    relayStatus = controlRelay(temp, desiredTemp);
+//
+//    if (temp != oldTemp){
+//      refreshDisplay = true;
+//      oldTemp = temp;
+//    }
+//    else if (relayStatus != oldRelayStatus){
+//      refreshDisplay = true;
+//      oldRelayStatus = relayStatus;
+//    }
+//  }
+//
+//  if (refreshDisplay == true){
+//    printDisplay(displayMode);
+//  }
+
+  // PID
+  oldInput = input;
+  input = checkTemp();
+  myPID.Compute();
+
+  /************************************************
+   * turn the output pin on/off based on pid output
+   ************************************************/
+  unsigned long now = millis();
+  if (now - windowStartTime > windowSize){
+    //time to shift the Relay Window
+    windowStartTime += windowSize;
+  }
+  oldRelayStatus = relayStatus;
+  if (input == 0.00){
+    digitalWrite(RelayPin,LOW);
+    relayStatus = false;
+  }
+  else if (output > now - windowStartTime){
+    digitalWrite(RelayPin,HIGH);
+    relayStatus = true;
+  }
+  else {
+    digitalWrite(RelayPin,LOW);
+    relayStatus = false;
+  }
+
+  if (relayStatus != oldRelayStatus){
+    printDisplay(displayMode, input, relayStatus);
+  }
+  if (input != oldInput){
+    printDisplay(displayMode, input, relayStatus);
+  }
+
+}


// Timing
long previousMillis = 0;
long tempInterval = 2000;

// Defaults
int displayMode = 0;
float temp = 0;
float oldTemp = 0;
boolean relayStatus = false;
boolean oldRelayStatus = false;
boolean refreshDisplay = true;


// Sketch to use an Arduino as tempcontroller
// in an Expobar Brewtus II espresso machine

//--> Import libraries <--
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <PID_v1.h>


//--> Setup variables <--
