Timing.ino 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // FILE: Timing.ino
  3. // AUTHOR: Rob Tillaart
  4. // VERSION: 0.0.3
  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. // 0.0.3 2020-02-20 added timing measurement of setResolution
  12. #include <OneWire.h>
  13. #include <DallasTemperature.h>
  14. #define ONE_WIRE_BUS 2
  15. OneWire oneWire(ONE_WIRE_BUS);
  16. DallasTemperature sensor(&oneWire);
  17. uint32_t start, stop;
  18. void setup()
  19. {
  20. Serial.begin(9600);
  21. Serial.println(__FILE__);
  22. Serial.print("DallasTemperature Library version: ");
  23. Serial.println(DALLASTEMPLIBVERSION);
  24. sensor.begin();
  25. }
  26. void loop()
  27. {
  28. float ti[4] = { 94, 188, 375, 750 };
  29. Serial.println();
  30. Serial.println("Test takes about 30 seconds for 4 resolutions");
  31. Serial.println("RES\tTIME\tACTUAL\tGAIN");
  32. for (int r = 9; r < 13; r++)
  33. {
  34. start = micros();
  35. sensor.setResolution(r);
  36. Serial.println(micros() - start);
  37. start = micros();
  38. sensor.setResolution(r);
  39. Serial.println(micros() - start);
  40. uint32_t duration = run(20);
  41. float avgDuration = duration / 20.0;
  42. Serial.print(r);
  43. Serial.print("\t");
  44. Serial.print(ti[r - 9]);
  45. Serial.print("\t");
  46. Serial.print(avgDuration, 2);
  47. Serial.print("\t");
  48. Serial.print(avgDuration * 100 / ti[r - 9], 1);
  49. Serial.println("%");
  50. }
  51. delay(1000);
  52. }
  53. uint32_t run(int runs)
  54. {
  55. float t;
  56. start = millis();
  57. for (int i = 0; i < runs; i++)
  58. {
  59. sensor.requestTemperatures();
  60. t = sensor.getTempCByIndex(0);
  61. }
  62. stop = millis();
  63. return stop - start;
  64. }