Advertisement
Guest User

Arduino temp. code

a guest
Mar 14th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. // PREVERI PINE ARDUINO -< ATTINY - PREDVSEM 9 IN 8!!!
  2. #include <OneWire.h>
  3. #include <LiquidCrystal.h>
  4. LiquidCrystal lcd(9, 8, 6, 5, 4, 3);
  5. //int DS18S20_Pin = A2; //DS18S20 Signal pin
  6. //pinMode(DS18S20_Pin, INPUT);
  7.  
  8.  
  9. //Temperature chip i/o
  10. OneWire ds(2); // on digital pin 2
  11. //OneWire ds(A2); // on digital pin 2
  12.  
  13. void setup(void) {
  14. // Serial.begin(9600);
  15. lcd.begin(16, 2);
  16. // pinMode(DS18S20_Pin, INPUT);
  17. }
  18.  
  19. void loop(void) {
  20. // digitalWrite(backLight, HIGH);
  21. // set the cursor to column 0, line 1
  22. // (note: line 1 is the second row, since counting begins with 0):
  23. float temperature = getTemp();
  24. lcd.setCursor(0, 0);
  25. lcd.print("Temp.: ");
  26. lcd.setCursor(7, 0);
  27. lcd.print(temperature);
  28. // lcd.setCursor(13, 0);
  29. // lcd.print((char)239);
  30. // lcd.setCursor(14, 0);
  31. // lcd.print("C");
  32. lcd.setCursor(0, 1);
  33. if (temperature < 20) {
  34. lcd.print("Zebeeee :(");
  35. } else if (temperature > 25) {
  36. lcd.print("Vroceee :(");
  37. } else {
  38. lcd.print("Milina! :)");
  39. }
  40. // Serial.print("Temperatura: ");
  41. // Serial.println(temperature);
  42. delay(2000); //just here to slow down the output so it is easier to read
  43. // lcd.setCursor(0, 1);
  44. // print the number of seconds since reset:
  45. // lcd.print(millis()/1000);
  46. }
  47.  
  48. float getTemp(){
  49. //returns the temperature from one DS18S20 in DEG Celsius
  50.  
  51. byte data[12];
  52. byte addr[8];
  53.  
  54. if ( !ds.search(addr)) {
  55. //no more sensors on chain, reset search
  56. ds.reset_search();
  57. return -1000;
  58. }
  59.  
  60. if ( OneWire::crc8( addr, 7) != addr[7]) {
  61. // Serial.println("CRC is not valid!");
  62. return -2000;
  63. }
  64.  
  65. if ( addr[0] != 0x10 && addr[0] != 0x28) {
  66. // Serial.print("Device is not recognized");
  67. return -3000;
  68. }
  69.  
  70. ds.reset();
  71. ds.select(addr);
  72. ds.write(0x44,1); // start conversion, with parasite power on at the end
  73.  
  74. byte present = ds.reset();
  75. ds.select(addr);
  76. // test
  77. for (int j = 0; j < 8; j++) {
  78. // Serial.print(addr[j],DEC);
  79. // Serial.print("/");
  80. // Serial.print(addr[j],HEX);
  81. // Serial.print(" ");
  82. }
  83. // test konec
  84. ds.write(0xBE); // Read Scratchpad
  85.  
  86.  
  87. for (int i = 0; i < 9; i++) { // we need 9 bytes
  88. data[i] = ds.read();
  89. }
  90.  
  91. ds.reset_search();
  92.  
  93. byte MSB = data[1];
  94. byte LSB = data[0];
  95.  
  96. float tempRead = ((MSB << 8) | LSB); //using two's compliment
  97. float TemperatureSum = tempRead / 16;
  98.  
  99. return TemperatureSum;
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement