to github
[svn42.git] / rf433ctl / DallasTemperature / examples / Multiple / Multiple.pde
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 3
6 #define TEMPERATURE_PRECISION 9
7
8 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
9 OneWire oneWire(ONE_WIRE_BUS);
10
11 // Pass our oneWire reference to Dallas Temperature. 
12 DallasTemperature sensors(&oneWire);
13
14 // arrays to hold device addresses
15 DeviceAddress insideThermometer, outsideThermometer;
16
17 void setup(void)
18 {
19   // start serial port
20   Serial.begin(9600);
21   Serial.println("Dallas Temperature IC Control Library Demo");
22
23   // Start up the library
24   sensors.begin();
25
26   // locate devices on the bus
27   Serial.print("Locating devices...");
28   Serial.print("Found ");
29   Serial.print(sensors.getDeviceCount(), DEC);
30   Serial.println(" devices.");
31
32   // report parasite power requirements
33   Serial.print("Parasite power is: "); 
34   if (sensors.isParasitePowerMode()) Serial.println("ON");
35   else Serial.println("OFF");
36
37   // assign address manually.  the addresses below will beed to be changed
38   // to valid device addresses on your bus.  device address can be retrieved
39   // by using either oneWire.search(deviceAddress) or individually via
40   // sensors.getAddress(deviceAddress, index)
41   //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
42   //outsideThermometer   = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
43
44   // search for devices on the bus and assign based on an index.  ideally,
45   // you would do this to initially discover addresses on the bus and then 
46   // use those addresses and manually assign them (see above) once you know 
47   // the devices on your bus (and assuming they don't change).
48   // 
49   // method 1: by index
50   if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
51   if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); 
52
53   // method 2: search()
54   // search() looks for the next device. Returns 1 if a new address has been
55   // returned. A zero might mean that the bus is shorted, there are no devices, 
56   // or you have already retrieved all of them.  It might be a good idea to 
57   // check the CRC to make sure you didn't get garbage.  The order is 
58   // deterministic. You will always get the same devices in the same order
59   //
60   // Must be called before search()
61   //oneWire.reset_search();
62   // assigns the first address found to insideThermometer
63   //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
64   // assigns the seconds address found to outsideThermometer
65   //if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
66
67   // show the addresses we found on the bus
68   Serial.print("Device 0 Address: ");
69   printAddress(insideThermometer);
70   Serial.println();
71
72   Serial.print("Device 1 Address: ");
73   printAddress(outsideThermometer);
74   Serial.println();
75
76   // set the resolution to 9 bit
77   sensors.setResolution(insideThermometer, 9);
78   sensors.setResolution(outsideThermometer, 9);
79
80   Serial.print("Device 0 Resolution: ");
81   Serial.print(sensors.getResolution(insideThermometer), DEC); 
82   Serial.println();
83
84   Serial.print("Device 1 Resolution: ");
85   Serial.print(sensors.getResolution(outsideThermometer), DEC); 
86   Serial.println();
87 }
88
89 // function to print a device address
90 void printAddress(DeviceAddress deviceAddress)
91 {
92   for (uint8_t i = 0; i < 8; i++)
93   {
94     // zero pad the address if necessary
95     if (deviceAddress[i] < 16) Serial.print("0");
96     Serial.print(deviceAddress[i], HEX);
97   }
98 }
99
100 // function to print the temperature for a device
101 void printTemperature(DeviceAddress deviceAddress)
102 {
103   float tempC = sensors.getTempC(deviceAddress);
104   Serial.print("Temp C: ");
105   Serial.print(tempC);
106   Serial.print(" Temp F: ");
107   Serial.print(DallasTemperature::toFahrenheit(tempC));
108 }
109
110 // function to print a device's resolution
111 void printResolution(DeviceAddress deviceAddress)
112 {
113   Serial.print("Resolution: ");
114   Serial.print(sensors.getResolution(deviceAddress));
115   Serial.println();    
116 }
117
118 // main function to print information about a device
119 void printData(DeviceAddress deviceAddress)
120 {
121   Serial.print("Device Address: ");
122   printAddress(deviceAddress);
123   Serial.print(" ");
124   printTemperature(deviceAddress);
125   Serial.println();
126 }
127
128 void loop(void)
129
130   // call sensors.requestTemperatures() to issue a global temperature 
131   // request to all devices on the bus
132   Serial.print("Requesting temperatures...");
133   sensors.requestTemperatures();
134   Serial.println("DONE");
135
136   // print the device information
137   printData(insideThermometer);
138   printData(outsideThermometer);
139 }
140 \r