Multiple.ino 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Include the libraries we need
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. // Data wire is plugged into port 2 on the Arduino
  5. #define ONE_WIRE_BUS 2
  6. #define TEMPERATURE_PRECISION 9
  7. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  8. OneWire oneWire(ONE_WIRE_BUS);
  9. // Pass our oneWire reference to Dallas Temperature.
  10. DallasTemperature sensors(&oneWire);
  11. // arrays to hold device addresses
  12. DeviceAddress insideThermometer, outsideThermometer;
  13. // Assign address manually. The addresses below will beed to be changed
  14. // to valid device addresses on your bus. Device address can be retrieved
  15. // by using either oneWire.search(deviceAddress) or individually via
  16. // sensors.getAddress(deviceAddress, index)
  17. // DeviceAddress insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
  18. // DeviceAddress outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
  19. void setup(void)
  20. {
  21. // start serial port
  22. Serial.begin(9600);
  23. Serial.println("Dallas Temperature IC Control Library Demo");
  24. // Start up the library
  25. sensors.begin();
  26. // locate devices on the bus
  27. Serial.print("Locating devices...");
  28. Serial.print("Found ");
  29. Serial.print(sensors.getDeviceCount(), DEC);
  30. Serial.println(" devices.");
  31. // report parasite power requirements
  32. Serial.print("Parasite power is: ");
  33. if (sensors.isParasitePowerMode()) Serial.println("ON");
  34. else Serial.println("OFF");
  35. // Search for devices on the bus and assign based on an index. Ideally,
  36. // you would do this to initially discover addresses on the bus and then
  37. // use those addresses and manually assign them (see above) once you know
  38. // the devices on your bus (and assuming they don't change).
  39. //
  40. // method 1: by index
  41. if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  42. if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
  43. // method 2: search()
  44. // search() looks for the next device. Returns 1 if a new address has been
  45. // returned. A zero might mean that the bus is shorted, there are no devices,
  46. // or you have already retrieved all of them. It might be a good idea to
  47. // check the CRC to make sure you didn't get garbage. The order is
  48. // deterministic. You will always get the same devices in the same order
  49. //
  50. // Must be called before search()
  51. //oneWire.reset_search();
  52. // assigns the first address found to insideThermometer
  53. //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
  54. // assigns the seconds address found to outsideThermometer
  55. //if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
  56. // show the addresses we found on the bus
  57. Serial.print("Device 0 Address: ");
  58. printAddress(insideThermometer);
  59. Serial.println();
  60. Serial.print("Device 1 Address: ");
  61. printAddress(outsideThermometer);
  62. Serial.println();
  63. // set the resolution to 9 bit per device
  64. sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
  65. sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
  66. Serial.print("Device 0 Resolution: ");
  67. Serial.print(sensors.getResolution(insideThermometer), DEC);
  68. Serial.println();
  69. Serial.print("Device 1 Resolution: ");
  70. Serial.print(sensors.getResolution(outsideThermometer), DEC);
  71. Serial.println();
  72. }
  73. // function to print a device address
  74. void printAddress(DeviceAddress deviceAddress)
  75. {
  76. for (uint8_t i = 0; i < 8; i++)
  77. {
  78. // zero pad the address if necessary
  79. if (deviceAddress[i] < 16) Serial.print("0");
  80. Serial.print(deviceAddress[i], HEX);
  81. }
  82. }
  83. // function to print the temperature for a device
  84. void printTemperature(DeviceAddress deviceAddress)
  85. {
  86. float tempC = sensors.getTempC(deviceAddress);
  87. if(tempC == DEVICE_DISCONNECTED_C)
  88. {
  89. Serial.println("Error: Could not read temperature data");
  90. return;
  91. }
  92. Serial.print("Temp C: ");
  93. Serial.print(tempC);
  94. Serial.print(" Temp F: ");
  95. Serial.print(DallasTemperature::toFahrenheit(tempC));
  96. }
  97. // function to print a device's resolution
  98. void printResolution(DeviceAddress deviceAddress)
  99. {
  100. Serial.print("Resolution: ");
  101. Serial.print(sensors.getResolution(deviceAddress));
  102. Serial.println();
  103. }
  104. // main function to print information about a device
  105. void printData(DeviceAddress deviceAddress)
  106. {
  107. Serial.print("Device Address: ");
  108. printAddress(deviceAddress);
  109. Serial.print(" ");
  110. printTemperature(deviceAddress);
  111. Serial.println();
  112. }
  113. /*
  114. Main function, calls the temperatures in a loop.
  115. */
  116. void loop(void)
  117. {
  118. // call sensors.requestTemperatures() to issue a global temperature
  119. // request to all devices on the bus
  120. Serial.print("Requesting temperatures...");
  121. sensors.requestTemperatures();
  122. Serial.println("DONE");
  123. // print the device information
  124. printData(insideThermometer);
  125. printData(outsideThermometer);
  126. }