Advertisement
Guest User

Arduino temp. code

a guest
Mar 14th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <LiquidCrystal.h>
  3. LiquidCrystal lcd(9, 8, 6, 5, 4, 3);
  4.  
  5. //Temperature chip i/o
  6. OneWire ds(2); // on digital pin 2
  7.  
  8. void setup(void) {
  9. lcd.begin(16, 2);
  10. }
  11.  
  12. void loop(void) {
  13. float temperature = getTemp();
  14. lcd.setCursor(0, 0);
  15. lcd.print("Temp.: ");
  16. lcd.setCursor(7, 0);
  17. lcd.print(temperature);
  18. lcd.setCursor(0, 1);
  19. if (temperature < 20) {
  20. lcd.print("Zebeeee :(");
  21. } else if (temperature > 25) {
  22. lcd.print("Vroceee :(");
  23. } else {
  24. lcd.print("Milina! :)");
  25. }
  26. delay(2000); //just here to slow down the output so it is easier to read
  27. }
  28.  
  29. float getTemp(){
  30. //returns the temperature from one DS18S20 in DEG Celsius
  31. byte data[12];
  32. byte addr[8]={0x28, 0x79, 0x02, 0xB9, 0x04, 0x00, 0x00, 0x0F};
  33. ds.reset();
  34. ds.select(addr);
  35. ds.write(0x44,1); // start conversion, with parasite power on at the end
  36. byte present = ds.reset();
  37. ds.select(addr);
  38. ds.write(0xBE); // Read Scratchpad
  39. for (int i = 0; i < 9; i++) { // we need 9 bytes
  40. data[i] = ds.read();
  41. }
  42. ds.reset_search();
  43. byte MSB = data[1];
  44. byte LSB = data[0];
  45. float tempRead = ((MSB << 8) | LSB); //using two's compliment
  46. float TemperatureSum = tempRead / 16;
  47. return TemperatureSum;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement