e0884ea39f76271cd468b5716120d6f867bc8537
[svn42.git] / rf433ctl / DallasTemperature / examples / Alarm / Alarm.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 addresses
14 DeviceAddress insideThermometer, outsideThermometer;
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   // Start up the library
23   sensors.begin();
24   
25   // locate devices on the bus
26   Serial.print("Found ");
27   Serial.print(sensors.getDeviceCount(), DEC);
28   Serial.println(" devices.");
29
30   // search for devices on the bus and assign based on an index.
31   if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
32   if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); 
33
34   // show the addresses we found on the bus
35   Serial.print("Device 0 Address: ");
36   printAddress(insideThermometer);
37   Serial.println();
38
39   Serial.print("Device 0 Alarms: ");
40   printAlarms(insideThermometer);
41   Serial.println();
42   
43   Serial.print("Device 1 Address: ");
44   printAddress(outsideThermometer);
45   Serial.println();
46
47   Serial.print("Device 1 Alarms: ");
48   printAlarms(outsideThermometer);
49   Serial.println();
50   
51   Serial.println("Setting alarm temps...");
52
53   // alarm when temp is higher than 30C
54   sensors.setHighAlarmTemp(insideThermometer, 30);
55   
56   // alarm when temp is lower than -10C
57   sensors.setLowAlarmTemp(insideThermometer, -10);
58   
59   // alarm when temp is higher than 31C
60   sensors.setHighAlarmTemp(outsideThermometer, 31);
61   
62   // alarn when temp is lower than 27C
63   sensors.setLowAlarmTemp(outsideThermometer, 27);
64   
65   Serial.print("New Device 0 Alarms: ");
66   printAlarms(insideThermometer);
67   Serial.println();
68   
69   Serial.print("New Device 1 Alarms: ");
70   printAlarms(outsideThermometer);
71   Serial.println();
72 }
73
74 // function to print a device address
75 void printAddress(DeviceAddress deviceAddress)
76 {
77   for (uint8_t i = 0; i < 8; i++)
78   {
79     if (deviceAddress[i] < 16) Serial.print("0");
80     Serial.print(deviceAddress[i], HEX);
81   }
82 }
83
84 // function to print the temperature for a device
85 void printTemperature(DeviceAddress deviceAddress)
86 {
87   float tempC = sensors.getTempC(deviceAddress);
88   Serial.print("Temp C: ");
89   Serial.print(tempC);
90   Serial.print(" Temp F: ");
91   Serial.print(DallasTemperature::toFahrenheit(tempC));
92 }
93
94 void printAlarms(uint8_t deviceAddress[])
95 {
96   char temp;
97   temp = sensors.getHighAlarmTemp(deviceAddress);
98   Serial.print("High Alarm: ");
99   Serial.print(temp, DEC);
100   Serial.print("C/");
101   Serial.print(DallasTemperature::toFahrenheit(temp));
102   Serial.print("F | Low Alarm: ");
103   temp = sensors.getLowAlarmTemp(deviceAddress);
104   Serial.print(temp, DEC);
105   Serial.print("C/");
106   Serial.print(DallasTemperature::toFahrenheit(temp));
107   Serial.print("F");
108 }
109
110 // main function to print information about a device
111 void printData(DeviceAddress deviceAddress)
112 {
113   Serial.print("Device Address: ");
114   printAddress(deviceAddress);
115   Serial.print(" ");
116   printTemperature(deviceAddress);
117   Serial.println();
118 }
119
120 void checkAlarm(DeviceAddress deviceAddress)
121 {
122   if (sensors.hasAlarm(deviceAddress))
123   {
124     Serial.print("ALARM: ");
125     printData(deviceAddress);
126   }
127 }
128
129 void loop(void)
130
131   // call sensors.requestTemperatures() to issue a global temperature 
132   // request to all devices on the bus
133   Serial.print("Requesting temperatures...");
134   sensors.requestTemperatures();
135   Serial.println("DONE");
136
137   // Method 1:
138   // check each address individually for an alarm condition
139   checkAlarm(insideThermometer);
140   checkAlarm(outsideThermometer);
141 /*
142   // Alternate method:
143   // Search the bus and iterate through addresses of devices with alarms
144   
145   // space for the alarm device's address
146   DeviceAddress alarmAddr;
147
148   Serial.println("Searching for alarms...");
149   
150   // resetAlarmSearch() must be called before calling alarmSearch()
151   sensors.resetAlarmSearch();
152   
153   // alarmSearch() returns 0 when there are no devices with alarms
154   while (sensors.alarmSearch(alarmAddr))
155   {
156     Serial.print("ALARM: ");
157     printData(alarmAddr);
158   }
159 */
160
161 }
162 \r