063d265a5e379f9d3b5485454227b2bffeaeec99
[svn42.git] / rf433ctl / DallasTemperature / examples / Tester / Tester.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 int numberOfDevices; // Number of temperature devices found
15
16 DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
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   // Grab a count of devices on the wire
28   numberOfDevices = sensors.getDeviceCount();
29   
30   // locate devices on the bus
31   Serial.print("Locating devices...");
32   
33   Serial.print("Found ");
34   Serial.print(numberOfDevices, DEC);
35   Serial.println(" devices.");
36
37   // report parasite power requirements
38   Serial.print("Parasite power is: "); 
39   if (sensors.isParasitePowerMode()) Serial.println("ON");
40   else Serial.println("OFF");
41   
42   // Loop through each device, print out address
43   for(int i=0;i<numberOfDevices; i++)
44   {
45     // Search the wire for address
46     if(sensors.getAddress(tempDeviceAddress, i))
47         {
48                 Serial.print("Found device ");
49                 Serial.print(i, DEC);
50                 Serial.print(" with address: ");
51                 printAddress(tempDeviceAddress);
52                 Serial.println();
53                 
54                 Serial.print("Setting resolution to ");
55                 Serial.println(TEMPERATURE_PRECISION,DEC);
56                 
57                 // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
58                 sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
59                 
60                  Serial.print("Resolution actually set to: ");
61                 Serial.print(sensors.getResolution(tempDeviceAddress), DEC); 
62                 Serial.println();
63         }else{
64                 Serial.print("Found ghost device at ");
65                 Serial.print(i, DEC);
66                 Serial.print(" but could not detect address. Check power and cabling");
67         }
68   }
69
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   
98   // Loop through each device, print out temperature data
99   for(int i=0;i<numberOfDevices; i++)
100   {
101     // Search the wire for address
102     if(sensors.getAddress(tempDeviceAddress, i))
103         {
104                 // Output the device ID
105                 Serial.print("Temperature for device: ");
106                 Serial.println(i,DEC);
107                 
108                 // It responds almost immediately. Let's print out the data
109                 printTemperature(tempDeviceAddress); // Use a simple function to print out the data
110         } 
111         //else ghost device! Check your power requirements and cabling
112         
113   }
114 }
115
116 // function to print a device address
117 void printAddress(DeviceAddress deviceAddress)
118 {
119   for (uint8_t i = 0; i < 8; i++)
120   {
121     if (deviceAddress[i] < 16) Serial.print("0");
122     Serial.print(deviceAddress[i], HEX);
123   }
124 }\r