 
   if (OneWire::crc8(addr, 7) != addr[7]) {
       Serial.println("CRC is not valid!");
-      return;
+      return -1000;
   }
 
   // the first ROM byte indicates which chip
   int type_s = getChipType(addr[0]);
+  
 
   if(!ds.reset()) {
     Serial.println("Failed to reset");
-    return;
+    return -1000;
   }
   ds.select(addr);
   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
-
-  delay(DELAY);     // maybe 750ms is enough, maybe not
+  
+  startTime = millis();
+  while(millis() - startTime < 1000);
+  // maybe 750ms is enough, maybe not
   // we might do a ds.depower() here, but the reset will take care of it.
 
   present = ds.reset();

#include <OneWire.h>
#include <PID_v1.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//

#define SENSOR_PIN 10
#define DELAY 1000

class SensorThread: public Thread {
  public:
    float value;
    int pin;

  void run(){
    value = getTemp();
    runned();
  }
};

OneWire ds(SENSOR_PIN);  // on pin 10 (a 4.7K resistor is necessary)

//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=70.0, Ki=0.6, Kd=0.6;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 100; 
uint32_t PID_interval = 30000;
unsigned long windowStartTime;

SensorThread tempSensor = SensorThread();

// Instantiate a new ThreadController
ThreadController controller = ThreadController();

// This is the callback for the Timer
void timerCallback(){
  controller.run();
}

void setup(void) {  
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);

   // Configures Thread
  tempSensor.pin = SENSOR_PIN;
  tempSensor.setInterval(10000);

  // Add the Threads to our ThreadController
  controller.add(&tempSensor);

  Timer1.initialize(10000000);
  Timer1.attachInterrupt(timerCallback);
  Timer1.start();
  
  windowStartTime = millis();
  Setpoint = 28.0;
  myPID.SetOutputLimits(0, WindowSize); //tell the PID to range between 0 and the full window size
