Single.pde 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // arrays to hold device address
  10. DeviceAddress insideThermometer;
  11. void setup(void)
  12. {
  13. // start serial port
  14. Serial.begin(9600);
  15. Serial.println("Dallas Temperature IC Control Library Demo");
  16. // locate devices on the bus
  17. Serial.print("Locating devices...");
  18. sensors.begin();
  19. Serial.print("Found ");
  20. Serial.print(sensors.getDeviceCount(), DEC);
  21. Serial.println(" devices.");
  22. // report parasite power requirements
  23. Serial.print("Parasite power is: ");
  24. if (sensors.isParasitePowerMode()) Serial.println("ON");
  25. else Serial.println("OFF");
  26. // assign address manually. the addresses below will beed to be changed
  27. // to valid device addresses on your bus. device address can be retrieved
  28. // by using either oneWire.search(deviceAddress) or individually via
  29. // sensors.getAddress(deviceAddress, index)
  30. //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
  31. // Method 1:
  32. // search for devices on the bus and assign based on an index. ideally,
  33. // you would do this to initially discover addresses on the bus and then
  34. // use those addresses and manually assign them (see above) once you know
  35. // the devices on your bus (and assuming they don't change).
  36. if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  37. // method 2: search()
  38. // search() looks for the next device. Returns 1 if a new address has been
  39. // returned. A zero might mean that the bus is shorted, there are no devices,
  40. // or you have already retrieved all of them. It might be a good idea to
  41. // check the CRC to make sure you didn't get garbage. The order is
  42. // deterministic. You will always get the same devices in the same order
  43. //
  44. // Must be called before search()
  45. //oneWire.reset_search();
  46. // assigns the first address found to insideThermometer
  47. //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
  48. // show the addresses we found on the bus
  49. Serial.print("Device 0 Address: ");
  50. printAddress(insideThermometer);
  51. Serial.println();
  52. // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  53. sensors.setResolution(insideThermometer, 9);
  54. Serial.print("Device 0 Resolution: ");
  55. Serial.print(sensors.getResolution(insideThermometer), DEC);
  56. Serial.println();
  57. }
  58. // function to print the temperature for a device
  59. void printTemperature(DeviceAddress deviceAddress)
  60. {
  61. // method 1 - slower
  62. //Serial.print("Temp C: ");
  63. //Serial.print(sensors.getTempC(deviceAddress));
  64. //Serial.print(" Temp F: ");
  65. //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
  66. // method 2 - faster
  67. float tempC = sensors.getTempC(deviceAddress);
  68. Serial.print("Temp C: ");
  69. Serial.print(tempC);
  70. Serial.print(" Temp F: ");
  71. Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  72. }
  73. void loop(void)
  74. {
  75. // call sensors.requestTemperatures() to issue a global temperature
  76. // request to all devices on the bus
  77. Serial.print("Requesting temperatures...");
  78. sensors.requestTemperatures(); // Send the command to get temperatures
  79. Serial.println("DONE");
  80. // It responds almost immediately. Let's print out the data
  81. printTemperature(insideThermometer); // Use a simple function to print out the data
  82. }
  83. // function to print a device address
  84. void printAddress(DeviceAddress deviceAddress)
  85. {
  86. for (uint8_t i = 0; i < 8; i++)
  87. {
  88. if (deviceAddress[i] < 16) Serial.print("0");
  89. Serial.print(deviceAddress[i], HEX);
  90. }
  91. }