Advertisement
skycrane

Termosketch

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