Go Down

Topic: [SOLVED] 18B20 timing problem (Read 7 times) previous topic - next topic

robtillaart

#35
Aug 12, 2011, 01:38 pm Last Edit: Aug 12, 2011, 01:48 pm by robtillaart Reason: 1
Quote
i do not know the way for measure the response time of getTempCByIndex,


Code: [Select]

unsigned long before = millis();
temp = sensors.getTempCByIndex(0);
Serial.println(millis() - before);

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart


I modified your code a bit:
* added two lines in setup() for a proper start

* moved the serial.print(temp) into the conditional part (so only print after a new reading, to not get flooded with values)
* commented out this same serial.println(temp) as that causes the tx led to blink
* LED 13 blinks after every read (just to see that the code still works)

==> I have no blinking tx LED at my Arduino 2009 (no UNO nearby)

Please try this code..
Code: [Select]
#include <DallasTemperature.h>
#include <OneWire.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

float temp;
long previousMillis = 0;

void setup(void)
{
  pinMode(13, OUTPUT);
  sensors.begin();
  sensors.setResolution(12);
  sensors.setWaitForConversion(false);  // makes it async asincrono=non blocca il programma durante la lettura
  Serial.begin(9600);

  sensors.requestTemperatures(); // Send the command to get temperatures
  previousMillis = millis();
}

void loop(void)
{
  if (millis() - previousMillis > 750)   // mette la temperatura nella variabile temp dopo 750ms
  {
    temp = sensors.getTempCByIndex(0);
    sensors.requestTemperatures(); // Send the command to get temperatures
    previousMillis = millis(); // riparte il temporizzatore
    digitalWrite(13, !digitalRead(13));
    // Serial.println(temp);
  }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

#37
Aug 12, 2011, 02:58 pm Last Edit: Aug 12, 2011, 03:44 pm by robtillaart Reason: 1
Did some poking & tweaking in the Dallas Library to optimize fetching the temperature only.  To be applied as previous specified. The comments give the hard coded delays of the wire class.

Code: [Select]

// EXPERIMENTAL
void DallasTemperature::readScratchPadFast(uint8_t* deviceAddress, uint8_t* scratchPad)
{
 // send the command                      
 _wire->reset();                                //  1.5 millis
 if (devices > 1) _wire->select(deviceAddress);  // command + 8 bytes == 9 * 0.6 = 5.4 millis.
 else _wire->skip();                          // 1 Byte = 0.6 millis.  // needed for 1 device

 _wire->write(READSCRATCH);          // 1 BYTE = 0.6 millis
 scratchPad[0] = _wire->read();         // read only the temperature bytes
 scratchPad[1] = _wire->read();         // 2 * 0.6 = 1.2 millis
}

Results sofar one sensor:

1) readScratchPad()         sensors.getTempCByIndex(0); takes 28 millis()
2) readScratchPadFast()   sensors.getTempCByIndex(0); takes 18 millis()

Results sofar two sensors:

1) readScratchPad()         sensors.getTempCByIndex(0); takes 28 millis()
2) readScratchPadFast()   sensors.getTempCByIndex(0); takes 23 millis()


Conclusion:
There is time to be gained, however to get more one probably need to dive into the wire lib.



disclaimer - code still experimental
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Testato

#38
Aug 12, 2011, 06:08 pm Last Edit: Aug 12, 2011, 06:20 pm by Testato Reason: 1
i updated the .ccp but have always 28ms with 1 sensor
may send me your library ?

i used this code (sensor on pin16 (A2))

Code: [Select]
#include <DallasTemperature.h>
#include <OneWire.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 16
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// temp variable
float temp;

void setup(void)
{
 sensors.begin();
 sensors.setResolution(12);
 sensors.setWaitForConversion(false);  // makes it async asincrono=non blocca il programma durante la lettur
 Serial.begin(9600);
}


void loop(void)
{
unsigned long before = millis();
temp = sensors.getTempCByIndex(0);
Serial.println(millis() - before);
}
- [Guida] IDE - http://goo.gl/ln6glr
- [Lib] ST7032i LCD I2C - http://goo.gl/GNojT6
- [Lib] PCF8574+HD44780 LCD I2C - http://goo.gl/r7CstH

robtillaart

#39
Aug 12, 2011, 09:24 pm Last Edit: Aug 12, 2011, 09:29 pm by robtillaart Reason: 1
Quote
i updated the .ccp but have always 28ms with 1 sensor
may send me your library ?


sorry but no, too much (other) experimental code in it. As soon as it is stable I'll get it incorporated in the official library.

I went through my previous post and found this bug  (sorry)
Code: [Select]

// EXPERIMENTAL
bool DallasTemperature::isConnected(uint8_t* deviceAddress, uint8_t* scratchPad)
{
 readScratchPad(deviceAddress, scratchPad);  <<<<<<<<<<< should be readScratchPadFast(deviceAddress, scratchPad); of course
 return true;
}


Explains why you still get 28 millis, it is experimental code (no guarantees ;)

(I'll fix the code in the prev post too) done

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up