Advertisement
rossoreed

Amended Dallas Temp Sensor

Sep 30th, 2011
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1.  
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4.  
  5. // Data wire is plugged into port 2 on the Arduino
  6. #define ONE_WIRE_BUS 2
  7. #define TEMPERATURE_PRECISION 9
  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. uint8_t insideThermometer[8], outsideThermometer[8];
  17.  
  18. void setup(void)
  19. {
  20.   // start serial port
  21.   Serial.begin(9600);
  22.   Serial.println("Dallas Temperature IC Control Library Demo");
  23.  
  24.   // Start up the library
  25.   sensors.begin();
  26.  
  27.   // locate devices on the bus
  28.   Serial.print("Locating devices...");
  29.   Serial.print("Found ");
  30.   Serial.print(sensors.getDeviceCount(), DEC);
  31.   Serial.println(" devices.");
  32.  
  33.   // report parasite power requirements
  34.   Serial.print("Parasite power is: ");
  35.   if (sensors.isParasitePowerMode()) Serial.println("ON");
  36.   else Serial.println("OFF");
  37.  
  38.   // assign address manually.  the addresses below will beed to be changed
  39.   // to valid device addresses on your bus.  device address can be retrieved
  40.   // by using either sensors.search(deviceAddress) or individually via
  41.   // sensors.getAddress(deviceAddress, index)
  42.   insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
  43.   outsideThermometer   = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
  44.  
  45.   // search for devices on the bus and assign based on an index.  ideally,
  46.   // you would do this to initially discover addresses on the bus and then
  47.   // use those addresses and manually assign them (see above) once you know
  48.   // the devices on your bus (and assuming they don't change).
  49.   // if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  50.   // if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
  51.  
  52.   // show the addresses we found on the bus
  53.   //Serial.print("Device 0 Address: ");
  54.   //printAddress(insideThermometer);
  55.   //Serial.println();
  56.  
  57.   // Serial.print("Device 1 Address: ");
  58.   //printAddress(outsideThermometer);
  59.   //Serial.println();
  60.  
  61.   // set the resolution to 9 bit
  62.   sensors.setResolution(insideThermometer, 9);
  63.   sensors.setResolution(outsideThermometer, 9);
  64.  
  65.   Serial.print("Device 0 Resolution: ");
  66.   Serial.print(sensors.getResolution(insideThermometer), DEC);
  67.   Serial.println();
  68.  
  69.   Serial.print("Device 1 Resolution: ");
  70.   Serial.print(sensors.getResolution(outsideThermometer), DEC);
  71.   Serial.println();
  72. }
  73.  
  74. // function to print a device address
  75. void printAddress(uint8_t deviceAddress[])
  76. {
  77.   for (uint8_t i = 0; i < 8; i++)
  78.   {
  79.     Serial.print(deviceAddress[i], HEX);
  80.     if (i < 7) Serial.print(" ");
  81.   }
  82. }
  83.  
  84. // function to print the temperature for a device
  85. void printTemperature(uint8_t deviceAddress[])
  86. {
  87.   // method 1 - slower
  88.   //Serial.print("Temp C: ");
  89.   //Serial.print(sensors.getTempC(deviceAddress));
  90.   //Serial.print(" Temp F: ");
  91.   //Serial.print(sensors.getTempF(deviceAddress));
  92.  
  93.   // method 2 - faster
  94.   float tempC = sensors.getTempC(deviceAddress);
  95.   Serial.print("Temp C: ");
  96.   Serial.print(tempC);
  97.   Serial.print(" Temp F: ");
  98.   Serial.print(DallasTemperature::toFahrenheit(tempC));
  99. }
  100.  
  101. // function to print a device's resolution
  102. void printResolution(uint8_t deviceAddress[])
  103. {
  104.   Serial.print("Resolution: ");
  105.   Serial.print(sensors.getResolution(deviceAddress));
  106.   Serial.println();    
  107. }
  108.  
  109. // main function to print information about a device
  110. void printData(uint8_t deviceAddress[])
  111. {
  112.   Serial.print("Device Address: ");
  113.   printAddress(deviceAddress);
  114.   Serial.print(" ");
  115.   printTemperature(deviceAddress);
  116.   Serial.println();
  117. }
  118.  
  119. void loop(void)
  120. {
  121.   // call sensors.requestTemperatures() to issue a global temperature
  122.   // request to all devices on the bus
  123.   Serial.print("Requesting temperatures...");
  124.   sensors.requestTemperatures();
  125.   Serial.println("DONE");
  126.  
  127.   // print the device information
  128.   printData(insideThermometer);
  129.   printData(outsideThermometer);
  130. }
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement