Alarm.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. // Data wire is plugged into port 2 on the Arduino
  4. #define ONE_WIRE_BUS 2
  5. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  6. OneWire oneWire(ONE_WIRE_BUS);
  7. // Pass our oneWire reference to Dallas Temperature.
  8. DallasTemperature sensors(&oneWire);
  9. // arrays to hold device addresses
  10. DeviceAddress insideThermometer, outsideThermometer;
  11. void setup(void)
  12. {
  13. // start serial port
  14. Serial.begin(9600);
  15. Serial.println("Dallas Temperature IC Control Library Demo");
  16. // Start up the library
  17. sensors.begin();
  18. // locate devices on the bus
  19. Serial.print("Found ");
  20. Serial.print(sensors.getDeviceCount(), DEC);
  21. Serial.println(" devices.");
  22. // search for devices on the bus and assign based on an index.
  23. if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  24. if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
  25. // show the addresses we found on the bus
  26. Serial.print("Device 0 Address: ");
  27. printAddress(insideThermometer);
  28. Serial.println();
  29. Serial.print("Device 0 Alarms: ");
  30. printAlarms(insideThermometer);
  31. Serial.println();
  32. Serial.print("Device 1 Address: ");
  33. printAddress(outsideThermometer);
  34. Serial.println();
  35. Serial.print("Device 1 Alarms: ");
  36. printAlarms(outsideThermometer);
  37. Serial.println();
  38. Serial.println("Setting alarm temps...");
  39. // alarm when temp is higher than 30C
  40. sensors.setHighAlarmTemp(insideThermometer, 30);
  41. // alarm when temp is lower than -10C
  42. sensors.setLowAlarmTemp(insideThermometer, -10);
  43. // alarm when temp is higher than 31C
  44. sensors.setHighAlarmTemp(outsideThermometer, 31);
  45. // alarn when temp is lower than 27C
  46. sensors.setLowAlarmTemp(outsideThermometer, 27);
  47. Serial.print("New Device 0 Alarms: ");
  48. printAlarms(insideThermometer);
  49. Serial.println();
  50. Serial.print("New Device 1 Alarms: ");
  51. printAlarms(outsideThermometer);
  52. Serial.println();
  53. }
  54. // function to print a device address
  55. void printAddress(DeviceAddress deviceAddress)
  56. {
  57. for (uint8_t i = 0; i < 8; i++)
  58. {
  59. if (deviceAddress[i] < 16) Serial.print("0");
  60. Serial.print(deviceAddress[i], HEX);
  61. }
  62. }
  63. // function to print the temperature for a device
  64. void printTemperature(DeviceAddress deviceAddress)
  65. {
  66. float tempC = sensors.getTempC(deviceAddress);
  67. Serial.print("Temp C: ");
  68. Serial.print(tempC);
  69. Serial.print(" Temp F: ");
  70. Serial.print(DallasTemperature::toFahrenheit(tempC));
  71. }
  72. void printAlarms(uint8_t deviceAddress[])
  73. {
  74. char temp;
  75. temp = sensors.getHighAlarmTemp(deviceAddress);
  76. Serial.print("High Alarm: ");
  77. Serial.print(temp, DEC);
  78. Serial.print("C/");
  79. Serial.print(DallasTemperature::toFahrenheit(temp));
  80. Serial.print("F | Low Alarm: ");
  81. temp = sensors.getLowAlarmTemp(deviceAddress);
  82. Serial.print(temp, DEC);
  83. Serial.print("C/");
  84. Serial.print(DallasTemperature::toFahrenheit(temp));
  85. Serial.print("F");
  86. }
  87. // main function to print information about a device
  88. void printData(DeviceAddress deviceAddress)
  89. {
  90. Serial.print("Device Address: ");
  91. printAddress(deviceAddress);
  92. Serial.print(" ");
  93. printTemperature(deviceAddress);
  94. Serial.println();
  95. }
  96. void checkAlarm(DeviceAddress deviceAddress)
  97. {
  98. if (sensors.hasAlarm(deviceAddress))
  99. {
  100. Serial.print("ALARM: ");
  101. printData(deviceAddress);
  102. }
  103. }
  104. void loop(void)
  105. {
  106. // call sensors.requestTemperatures() to issue a global temperature
  107. // request to all devices on the bus
  108. Serial.print("Requesting temperatures...");
  109. sensors.requestTemperatures();
  110. Serial.println("DONE");
  111. // Method 1:
  112. // check each address individually for an alarm condition
  113. checkAlarm(insideThermometer);
  114. checkAlarm(outsideThermometer);
  115. /*
  116. // Alternate method:
  117. // Search the bus and iterate through addresses of devices with alarms
  118. // space for the alarm device's address
  119. DeviceAddress alarmAddr;
  120. Serial.println("Searching for alarms...");
  121. // resetAlarmSearch() must be called before calling alarmSearch()
  122. sensors.resetAlarmSearch();
  123. // alarmSearch() returns 0 when there are no devices with alarms
  124. while (sensors.alarmSearch(alarmAddr))
  125. {
  126. Serial.print("ALARM: ");
  127. printData(alarmAddr);
  128. }
  129. */
  130. }