         ds18b20.resetsearch();
         delay(250);
         
-        return;       
-    }
+        return;  
+    }     
     
-    // Actual temperature in celsius
-    actualTemperature = ds18b20.getTemperature();
-    
-    // Calculate PID output
-    myPID.Compute();
-  
     
     // Send event to the SparkCloud
     if (millis() - lastEventTimestamp >= 1000) {
+
+        // Actual temperature in celsius
+        actualTemperature = ds18b20.getTemperature();
+
+        // Compute PID
+        if(myPID.Compute()) {
+            
+            // Control relay
+            analogWrite(relayPin, pidOutput);
+        }
         
         // Convert values for output
         // convert pidOutput to 0-100

    myPID.SetMode(AUTOMATIC);
    
    // Last event timestamp
    lastEventTimestamp = 0;
}
 
 // Loop
void loop() {
    
    // Searching for the ds18b20 device
    if(!ds18b20.search()){
        
        // Log to the serial
        Serial.println("No more addresses.");

double pointTemperature, actualTemperature, pidOutput;

// PID: Specify the links and initial tuning parameters
PID myPID(&actualTemperature, &pidOutput, &pointTemperature, 25, 1000, 9, DIRECT);

// Timestamp of the last status events
unsigned long lastEventTimestamp;
