Tester.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #define TEMPERATURE_PRECISION 9 // Lower resolution
  6. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  7. OneWire oneWire(ONE_WIRE_BUS);
  8. // Pass our oneWire reference to Dallas Temperature.
  9. DallasTemperature sensors(&oneWire);
  10. int numberOfDevices; // Number of temperature devices found
  11. DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
  12. void setup(void)
  13. {
  14. // start serial port
  15. Serial.begin(9600);
  16. Serial.println("Dallas Temperature IC Control Library Demo");
  17. // Start up the library
  18. sensors.begin();
  19. // Grab a count of devices on the wire
  20. numberOfDevices = sensors.getDeviceCount();
  21. // locate devices on the bus
  22. Serial.print("Locating devices...");
  23. Serial.print("Found ");
  24. Serial.print(numberOfDevices, DEC);
  25. Serial.println(" devices.");
  26. // report parasite power requirements
  27. Serial.print("Parasite power is: ");
  28. if (sensors.isParasitePowerMode()) Serial.println("ON");
  29. else Serial.println("OFF");
  30. // Loop through each device, print out address
  31. for(int i=0;i<numberOfDevices; i++)
  32. {
  33. // Search the wire for address
  34. if(sensors.getAddress(tempDeviceAddress, i))
  35. {
  36. Serial.print("Found device ");
  37. Serial.print(i, DEC);
  38. Serial.print(" with address: ");
  39. printAddress(tempDeviceAddress);
  40. Serial.println();
  41. Serial.print("Setting resolution to ");
  42. Serial.println(TEMPERATURE_PRECISION, DEC);
  43. // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
  44. sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
  45. Serial.print("Resolution actually set to: ");
  46. Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
  47. Serial.println();
  48. }else{
  49. Serial.print("Found ghost device at ");
  50. Serial.print(i, DEC);
  51. Serial.print(" but could not detect address. Check power and cabling");
  52. }
  53. }
  54. }
  55. // function to print the temperature for a device
  56. void printTemperature(DeviceAddress deviceAddress)
  57. {
  58. // method 1 - slower
  59. //Serial.print("Temp C: ");
  60. //Serial.print(sensors.getTempC(deviceAddress));
  61. //Serial.print(" Temp F: ");
  62. //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
  63. // method 2 - faster
  64. float tempC = sensors.getTempC(deviceAddress);
  65. if(tempC == DEVICE_DISCONNECTED_C)
  66. {
  67. Serial.println("Error: Could not read temperature data");
  68. return;
  69. }
  70. Serial.print("Temp C: ");
  71. Serial.print(tempC);
  72. Serial.print(" Temp F: ");
  73. Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  74. }
  75. void loop(void)
  76. {
  77. // call sensors.requestTemperatures() to issue a global temperature
  78. // request to all devices on the bus
  79. Serial.print("Requesting temperatures...");
  80. sensors.requestTemperatures(); // Send the command to get temperatures
  81. Serial.println("DONE");
  82. // Loop through each device, print out temperature data
  83. for(int i=0;i<numberOfDevices; i++)
  84. {
  85. // Search the wire for address
  86. if(sensors.getAddress(tempDeviceAddress, i))
  87. {
  88. // Output the device ID
  89. Serial.print("Temperature for device: ");
  90. Serial.println(i,DEC);
  91. // It responds almost immediately. Let's print out the data
  92. printTemperature(tempDeviceAddress); // Use a simple function to print out the data
  93. }
  94. //else ghost device! Check your power requirements and cabling
  95. }
  96. }
  97. // function to print a device address
  98. void printAddress(DeviceAddress deviceAddress)
  99. {
  100. for (uint8_t i = 0; i < 8; i++)
  101. {
  102. if (deviceAddress[i] < 16) Serial.print("0");
  103. Serial.print(deviceAddress[i], HEX);
  104. }
  105. }