Tester.pde 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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
  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. Serial.print("Temp C: ");
  66. Serial.print(tempC);
  67. Serial.print(" Temp F: ");
  68. Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  69. }
  70. void loop(void)
  71. {
  72. // call sensors.requestTemperatures() to issue a global temperature
  73. // request to all devices on the bus
  74. Serial.print("Requesting temperatures...");
  75. sensors.requestTemperatures(); // Send the command to get temperatures
  76. Serial.println("DONE");
  77. // Loop through each device, print out temperature data
  78. for(int i=0;i<numberOfDevices; i++)
  79. {
  80. // Search the wire for address
  81. if(sensors.getAddress(tempDeviceAddress, i))
  82. {
  83. // Output the device ID
  84. Serial.print("Temperature for device: ");
  85. Serial.println(i,DEC);
  86. // It responds almost immediately. Let's print out the data
  87. printTemperature(tempDeviceAddress); // Use a simple function to print out the data
  88. }
  89. //else ghost device! Check your power requirements and cabling
  90. }
  91. }
  92. // function to print a device address
  93. void printAddress(DeviceAddress deviceAddress)
  94. {
  95. for (uint8_t i = 0; i < 8; i++)
  96. {
  97. if (deviceAddress[i] < 16) Serial.print("0");
  98. Serial.print(deviceAddress[i], HEX);
  99. }
  100. }