Timing.ino 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // FILE: Timing.ino
  3. // AUTHOR: Rob Tillaart
  4. // VERSION: 0.0.2
  5. // PURPOSE: show performance of DallasTemperature lib
  6. // compared to datasheet times per resolution
  7. //
  8. // HISTORY:
  9. // 0.0.1 = 2017-07-25 initial version
  10. // 0.0.2 = 2020-02-13 updates to work with current lib version
  11. #include <OneWire.h>
  12. #include <DallasTemperature.h>
  13. #define ONE_WIRE_BUS 2
  14. OneWire oneWire(ONE_WIRE_BUS);
  15. DallasTemperature sensor(&oneWire);
  16. uint32_t start, stop;
  17. void setup()
  18. {
  19. Serial.begin(9600);
  20. Serial.println(__FILE__);
  21. Serial.print("DallasTemperature Library version: ");
  22. Serial.println(DALLASTEMPLIBVERSION);
  23. sensor.begin();
  24. }
  25. void loop()
  26. {
  27. float ti[4] = { 94, 188, 375, 750 };
  28. Serial.println();
  29. Serial.println("Test takes about 30 seconds for 4 resolutions");
  30. Serial.println("RES\tTIME\tACTUAL\tGAIN");
  31. for (int r = 9; r < 13; r++)
  32. {
  33. sensor.setResolution(r);
  34. uint32_t duration = run(20);
  35. float avgDuration = duration / 20.0;
  36. Serial.print(r);
  37. Serial.print("\t");
  38. Serial.print(ti[r - 9]);
  39. Serial.print("\t");
  40. Serial.print(avgDuration, 2);
  41. Serial.print("\t");
  42. Serial.print(avgDuration * 100 / ti[r - 9], 1);
  43. Serial.println("%");
  44. }
  45. delay(1000);
  46. }
  47. uint32_t run(int runs)
  48. {
  49. float t;
  50. start = millis();
  51. for (int i = 0; i < runs; i++)
  52. {
  53. sensor.requestTemperatures();
  54. t = sensor.getTempCByIndex(0);
  55. }
  56. stop = millis();
  57. return stop - start;
  58. }