Advertisement
Guest User

Untitled

a guest
Dec 24th, 2015
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3.  
  4. // Data wire is plugged into port 2 on the Arduino
  5. #define ONE_WIRE_BUS 10
  6. #define TEMPERATURE_PRECISION 8
  7. #define BAUD_RATE 9600
  8.  
  9. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  10. OneWire oneWire(ONE_WIRE_BUS);
  11.  
  12. // Pass our oneWire reference to Dallas Temperature.
  13. DallasTemperature sensors(&oneWire);
  14.  
  15. // arrays to hold device addresses
  16. DeviceAddress devices[10];
  17. int devicesFound = 0;
  18.  
  19. void setup(void)
  20. {
  21.   // start serial port
  22.   Serial.begin(BAUD_RATE);
  23.  
  24.   sensors.begin();
  25.  
  26.   devicesFound = sensors.getDeviceCount();
  27.  
  28.   // locate devices on the bus
  29.   Serial.print("Locating devices...");
  30.   Serial.print("Found ");
  31.   Serial.print(devicesFound, DEC);
  32.   Serial.println(" devices.");  
  33.  
  34.   // report parasite power requirements
  35.   Serial.print("Parasite power is: ");
  36.   if (sensors.isParasitePowerMode()) Serial.println("ON");
  37.   else Serial.println("OFF");
  38.  
  39.   for (int i = 0; i < devicesFound; i++)
  40.     if (!sensors.getAddress(devices[i], i))
  41.       Serial.println("Unable to find address for Device" + i);
  42.  
  43.   // show the addresses we found on the bus
  44.   for (int i = 0; i < devicesFound; i++)
  45.   {    
  46.     Serial.print("Device " + (String)i + " Address: ");
  47.     printAddress(devices[i]);
  48.     Serial.println();
  49.   }
  50.  
  51.   for (int i = 0; i < devicesFound; i++)
  52.     sensors.setResolution(devices[i], TEMPERATURE_PRECISION);
  53. }
  54.  
  55. // function to print a device address
  56. void printAddress(DeviceAddress deviceAddress)
  57. {
  58.   for (uint8_t i = 0; i < 8; i++)
  59.   {
  60.     // zero pad the address if necessary
  61.     if (deviceAddress[i] < 16) Serial.print("0");
  62.     Serial.print(deviceAddress[i], HEX);
  63.   }
  64. }
  65.  
  66. // function to print the temperature for a device
  67. void printTemperature(DeviceAddress deviceAddress)
  68. {
  69.   float tempC = sensors.getTempC(deviceAddress);
  70.   if (tempC < 10)
  71.     Serial.print("0");
  72.    
  73.   Serial.print(tempC);
  74. }
  75.  
  76. // function to print a device's resolution
  77. void printResolution(DeviceAddress deviceAddress)
  78. {
  79.   Serial.print("Resolution: ");
  80.   Serial.print(sensors.getResolution(deviceAddress));
  81.   Serial.println();    
  82. }
  83.  
  84. // main function to print information about a device
  85. void printData(DeviceAddress deviceAddress)
  86. {
  87.   Serial.print("Device Address: ");
  88.   printAddress(deviceAddress);
  89.   Serial.print(" ");
  90.   printTemperature(deviceAddress);
  91.   Serial.println();
  92. }
  93.  
  94. void loop(void)
  95. {
  96.   if (devicesFound == 0)
  97.   {
  98.      Serial.println("No devices found.");
  99.      return;
  100.   }
  101.  
  102.   int start=millis();
  103.   sensors.requestTemperatures();
  104.  
  105.   // print the device information
  106.   for (int i = 0; i < devicesFound; i++)
  107.   {
  108.     printTemperature(devices[i]);
  109.     if (i != devicesFound - 1)
  110.       Serial.print(" ");    
  111.   }  
  112.    
  113.   Serial.println();
  114.  
  115.   Serial.print(millis() - start);
  116.   Serial.println(" ms passed.");
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement