Advertisement
skycrane

Untitled

Sep 9th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. #include <OneWire.h>
  3.  
  4. // LCD=======================================================
  5. //initialize the library with the numbers of the interface pins
  6. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Defines LCD pins
  7. #define LCD_WIDTH 20
  8. #define LCD_HEIGHT 2
  9.  
  10. const int compStart = 9; // Defines Compressor pin
  11. const int fanStart = 8; // Defines fan-pin
  12.  
  13. const int lowThresh = 2640; //sets temp when compressor stops
  14. const int highThresh = 2650; //sets temp when compressor starts
  15.  
  16. const int lowFan = 2600; //sets temp when fan starts
  17. const int highFan = 2630; //sets temp when fan stops
  18.  
  19.  
  20. // DS18S20 Temperature chip i/o
  21. OneWire ds(10); // on pin 10
  22.  
  23. void setup(void) {
  24. // initialize inputs/outputs
  25. // start serial port
  26. Serial.begin(9600);
  27.  
  28. //Setup LCD 16x2 and display startup message
  29. lcd.begin(16, 2);
  30. lcd.print(" Smart Fridge");
  31. lcd.setCursor(0,1);
  32. lcd.print(" Starting Up");
  33. delay(1000);
  34. lcd.clear();
  35.  
  36. }
  37.  
  38. void loop(void) {
  39.  
  40. int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  41.  
  42. byte i;
  43. byte present = 0;
  44. byte data[12];
  45. byte addr[8];
  46. char* current_ds_addr = "";
  47.  
  48. if ( !ds.search(addr)) {
  49. Serial.print("No more addresses.\n");
  50. ds.reset_search();
  51. return;
  52. }
  53.  
  54. Serial.print("R=");
  55. for( i = 0; i < 8; i++) {
  56. Serial.print(addr[i], HEX);
  57. Serial.print(" ");
  58. }
  59.  
  60. if ( OneWire::crc8( addr, 7) != addr[7]) {
  61. Serial.print("CRC is not valid!\n");
  62. return;
  63. }
  64.  
  65. if ( addr[0] == 0x10) {
  66. Serial.print("Device is a DS18S20 family device.\n");
  67. }
  68. else if ( addr[0] == 0x28) {
  69. Serial.print("Device is a DS18B20 family device.\n");
  70. }
  71. else {
  72. Serial.print("Device family is not recognized: 0x");
  73. Serial.println(addr[0],HEX);
  74. return;
  75. }
  76.  
  77. ds.reset();
  78. ds.select(addr);
  79. ds.write(0x44,1); // start conversion, with parasite power on at the end
  80.  
  81. delay(4000); // maybe 750ms is enough, maybe not
  82. // we might do a ds.depower() here, but the reset will take care of it.
  83.  
  84. present = ds.reset();
  85. ds.select(addr);
  86. ds.write(0xBE); // Read Scratchpad
  87.  
  88. Serial.print("P=");
  89. Serial.print(present,HEX);
  90. Serial.print(" ");
  91. for ( i = 0; i < 9; i++) { // we need 9 bytes
  92. data[i] = ds.read();
  93. Serial.print(data[i], HEX);
  94. Serial.print(" ");
  95. }
  96. Serial.print(" CRC=");
  97. Serial.print( OneWire::crc8( data, 8), HEX);
  98. Serial.println();
  99.  
  100. LowByte = data[0];
  101. HighByte = data[1];
  102. TReading = (HighByte << 8) + LowByte;
  103. SignBit = TReading & 0x8000; // test most sig bit
  104. if (SignBit) // negative
  105. {
  106. TReading = (TReading ^ 0xffff) + 1; // 2's comp
  107. }
  108. Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
  109.  
  110. Whole = Tc_100 / 100; // separate off the whole and fractional portions
  111. Fract = Tc_100 % 100;
  112.  
  113.  
  114. if (SignBit) // If its negative
  115. {
  116. Serial.print("-");
  117. }
  118. Serial.print(Whole);
  119. Serial.print(".");
  120. if (Fract < 10)
  121. {
  122. Serial.print("0");
  123. }
  124. Serial.print(Fract);
  125.  
  126. Serial.print("\n");
  127.  
  128. //print onto LCD
  129. lcd.setCursor(0,0);
  130. lcd.print("Tmp:");
  131. lcd.print(Whole);
  132. lcd.print(".");
  133. lcd.print(Fract);
  134. lcd.setCursor(0,1);
  135. lcd.print("Fan:");
  136.  
  137. lcd.setCursor(10,0);
  138. for( i = 0; i < 8; i++) {
  139. lcd.print(addr[i], HEX);
  140.  
  141. if (Tc_100 > highThresh) {
  142. digitalWrite(compStart,HIGH);
  143. }
  144. else if (Tc_100 < lowThresh)
  145. digitalWrite(compStart,LOW);
  146. }
  147. if (Tc_100 < lowFan) {
  148. digitalWrite(fanStart,HIGH);
  149. }
  150. else if (Tc_100 > highFan) {
  151. digitalWrite(fanStart,LOW);
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement