DS18B20_no_delay sketch not working

This is a very weird issue I discovered today.
I have this temperature sensor, DS18B20, that works ok with the Arduino Yun when I use the classic sketch with delay() function (delay is needed between temperature convertions).

I did a modified version of the sketch using millis() and this doesn’t work, the sketch outputs negative temp (-0.02 celsuis). The wird thing is, this “DS18B20 no delay” sketch works fine on an Arduino Uno. I can’t come with ideas, why does this sketch work with Uno but not with Yun. I tried increasing time between convertions but didn’t work.

I’ll attach both codes, the one with delay and the millis() one, and also the OneWire library.

DS18B20_with_delay.ino

#include <OneWire.h>

OneWire  ds(8);  // on pin 10

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte data[12];
  byte addr[8];
  float celsius;
  
  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }
  
    
  ds.reset();
  ds.select(addr);
  ds.write(0x44,0);         // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
     }
  unsigned int raw = (data[1] << 8) | data[0];

    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  
  celsius = (float)raw / 16.0; //le resté 4 grados segun el termometro de la cocina
  
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.println(" Celsius");
  
 
}

DS18B20_no_delay.ino

/*Version modificada del ejemplo 
OneWire, esta version usa millis()
para no tener que usar delays en el
codigo.
*/
#include <OneWire.h>

//-----------Variables y constantes-------------------------

#define sensorPin 8

int estado_sen=0;
long sen_actual=0;

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

void setup(void) {
  Serial.begin(9600);
}

//-------------rutina principal-----------------------------

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  //maquina de estados para el sensor de temperatura DS18B20
  switch(estado_sen){
  
    case 0: if ( !ds.search(addr)) {
            Serial.println("No more addresses.");
            Serial.println();
            ds.reset_search();
            estado_sen=1;
            sen_actual=millis();
            break;
            }
            estado_sen=2;             
            break;
     
    case 1: if(millis()-sen_actual >= 250){
             estado_sen=0;   //se debe volver al estado 0 hasta agotar direcciones
                             //tal como lo hace la funcion return; en el codigo original
             }
            break;
             
    case 2:   if (OneWire::crc8(addr, 7) != addr[7]) {
              Serial.println("CRC is not valid!");
              return;
              }
              Serial.println();
              ds.reset();
              ds.select(addr);
              ds.write(0x44, 0);        // si se pone xx,1 se usa parasit power.
              estado_sen=3;
              sen_actual=millis();
              break;
              
    case 3: if(millis()-sen_actual >= 1500){
              present = ds.reset();
              ds.select(addr);    
              ds.write(0xBE);         // Read Scratchpad
            
              for ( i = 0; i < 9; i++) {           // we need 9 bytes
                data[i] = ds.read();
              }
            
              // Convert the data to actual temperature
              // because the result is a 16 bit signed integer, it should
              // be stored to an "int16_t" type, which is always 16 bits
              // even when compiled on a 32 bit processor.
              int16_t raw = (data[1] << 8) | data[0];
              if (type_s) {
                raw = raw << 3; // 9 bit resolution default
                if (data[7] == 0x10) {
                  // "count remain" gives full 12 bit resolution
                  raw = (raw & 0xFFF0) + 12 - data[6];
                }
              } else {
                byte cfg = (data[4] & 0x60);
                // at lower res, the low bits are undefined, so let's zero them
                if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
                else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
                else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
                //// default is 12 bit resolution, 750 ms conversion time
              }
              celsius = (float)raw / 16.0;
              Serial.print("Temperature = ");
              Serial.print(celsius);
              Serial.println(" Celsius");
              estado_sen=0;
              }
             break;     
              
   default: break;
  }
}

OneWire.zip (14.1 KB)