WaitForConversion.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. // Data wire is plugged into port 2 on the Arduino
  4. #define ONE_WIRE_BUS 2
  5. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  6. OneWire oneWire(ONE_WIRE_BUS);
  7. // Pass our oneWire reference to Dallas Temperature.
  8. DallasTemperature sensors(&oneWire);
  9. void setup(void)
  10. {
  11. // start serial port
  12. Serial.begin(115200);
  13. Serial.println("Dallas Temperature Control Library - Async Demo");
  14. Serial.println("\nDemo shows the difference in length of the call\n\n");
  15. // Start up the library
  16. sensors.begin();
  17. }
  18. void loop(void)
  19. {
  20. // Request temperature conversion (traditional)
  21. Serial.println("Before blocking requestForConversion");
  22. unsigned long start = millis();
  23. sensors.requestTemperatures();
  24. unsigned long stop = millis();
  25. Serial.println("After blocking requestForConversion");
  26. Serial.print("Time used: ");
  27. Serial.println(stop - start);
  28. // get temperature
  29. Serial.print("Temperature: ");
  30. Serial.println(sensors.getTempCByIndex(0));
  31. Serial.println("\n");
  32. // Request temperature conversion - non-blocking / async
  33. Serial.println("Before NON-blocking/async requestForConversion");
  34. start = millis();
  35. sensors.setWaitForConversion(false); // makes it async
  36. sensors.requestTemperatures();
  37. sensors.setWaitForConversion(true);
  38. stop = millis();
  39. Serial.println("After NON-blocking/async requestForConversion");
  40. Serial.print("Time used: ");
  41. Serial.println(stop - start);
  42. // 9 bit resolution by default
  43. // Note the programmer is responsible for the right delay
  44. // we could do something usefull here instead of the delay
  45. int resolution = 9;
  46. delay(750/ (1 << (12-resolution)));
  47. // get temperature
  48. Serial.print("Temperature: ");
  49. Serial.println(sensors.getTempCByIndex(0));
  50. Serial.println("\n\n\n\n");
  51. delay(5000);
  52. }