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