Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 12th, 2013  |  syntax: None  |  size: 3.76 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. **I am have been using the following code to do a serial monitor of the temp but would like to display to LCD.**
  2.  
  3. #include <OneWire.h>
  4.  
  5. int SensorPin = 10;
  6.  
  7.  
  8. OneWire ds(SensorPin);
  9.  
  10. void setup(void) {
  11. Serial.begin(9600);
  12. }
  13.  
  14. void loop(void) {
  15. float temp = getTemp();
  16. Serial.println(temp);
  17.  
  18. delay(100);
  19.  
  20. }
  21.  
  22.  
  23. float getTemp(){
  24.   //returns the temperature from one DS18S20 in DEG Celsius
  25.  
  26.   byte data[12];
  27.   byte addr[8];
  28.  
  29.   if ( !ds.search(addr)) {
  30.       //no more sensors on chain, reset search
  31.       ds.reset_search();
  32.       return -1000;
  33.   }
  34.  
  35.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  36.       Serial.println("CRC is not valid!");
  37.       return -1000;
  38.   }
  39.  
  40.   if ( addr[0] != 0x10 && addr[0] != 0x28) {
  41.       Serial.print("Device is not recognized");
  42.       return -1000;
  43.   }
  44.  
  45.   ds.reset();
  46.   ds.select(addr);
  47.   ds.write(0x44,1); // start conversion, with parasite power on at the end
  48.  
  49.   byte present = ds.reset();
  50.   ds.select(addr);    
  51.   ds.write(0xBE); // Read Scratchpad
  52.  
  53.  
  54.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  55.     data[i] = ds.read();
  56.   }
  57.  
  58.   ds.reset_search();
  59.  
  60.   byte MSB = data[1];
  61.   byte LSB = data[0];
  62.  
  63.   float tempRead = ((MSB << 8) | LSB); //using two's compliment
  64.   float TemperatureSum = tempRead / 16;
  65.  
  66.   return TemperatureSum;
  67.  
  68. }
  69.  
  70. **I have been trying to mesh it together with this code but with no success.**
  71.  
  72. //Jeremy Blum's Arduino Tutorial Series - Episode 13 - LCDs
  73. //Sample Code - LCD Temperature Display
  74. //http://www.jeremyblum.com
  75. //Debouncing code from Tutorial 2: http://jeremyblum.com/2011/01/10/arduino-tutorial-2-now-with-more-blinky-things/
  76. //I2C Code from Tutorial 7: http://jeremyblum.com/2011/02/13/arduino-tutorial-7-i2c-and-processing/
  77. //LiquidCrystal library by David A. Mellis, Limor Fried (http://www.ladyada.net), and Tom Igoe
  78. //http://www.arduino.cc/en/Tutorial/LiquidCrystal
  79.  
  80. //Include Wire I2C Library
  81. #include  <Wire.h>
  82. int temp_address = 72;
  83.  
  84. // include the library code:
  85. #include <LiquidCrystal.h>
  86.  
  87. // initialize the library with the numbers of the interface pins
  88. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  89.  
  90.  
  91. byte degree[8] = {
  92.   B00110,
  93.   B01001,
  94.   B01001,
  95.   B00110,
  96.   B00000,
  97.   B00000,
  98.   B00000,
  99. };
  100.  
  101. int switchPin = 8;
  102. boolean lastButton = LOW;
  103. boolean currentButton = LOW;
  104. boolean celcius = true;
  105.  
  106. void setup() {
  107.   //Mode Button
  108.   pinMode(switchPin, INPUT);
  109.  
  110.    //Create a Wire Object
  111.   Wire.begin();
  112.  
  113.   // set up the LCD's number of columns and rows:
  114.   lcd.begin(16, 2);
  115.   // Print a message to the LCD.
  116.   lcd.print("ROOM TEMPERATURE");
  117.  
  118.   lcd.setCursor(11,1);
  119.   lcd.print("mode>");
  120.  
  121.   //Make progress characters
  122.   lcd.createChar(0, degree);
  123. }
  124.  
  125. boolean debounce(boolean last)
  126. {
  127.   boolean current = digitalRead(switchPin);
  128.   if (last != current)
  129.   {
  130.     delay(5);
  131.     current = digitalRead(switchPin);
  132.   }
  133.   return current;
  134. }
  135.  
  136. void loop() {
  137.  
  138.   //Handle Temperature
  139.   //Send Request
  140.   Wire.beginTransmission(temp_address); //Start talking
  141.   Wire.send(0); //Ask for Register zero
  142.   Wire.endTransmission(); //Complete Transmission
  143.   Wire.requestFrom(temp_address, 1); //Request 1 Byte
  144.   while(Wire.available() == 0); //wait for response
  145.   int c = Wire.receive(); // Get the temp in c
  146.   //do some math...
  147.   int f = round(c*9.0/5.0 +32.0);
  148.  
  149.  
  150.   currentButton = debounce(lastButton);
  151.   if (lastButton == LOW && currentButton == HIGH)
  152.   {
  153.     if (celcius) celcius = false;
  154.     else celcius = true;
  155.    
  156.    //Erase old characters
  157.    lcd.setCursor(0,1);
  158.    lcd.print("           ");
  159.   }
  160.   lastButton = currentButton;
  161.  
  162.    
  163.  lcd.setCursor(0,1);
  164.  if (celcius)
  165.  {
  166.    lcd.print(c);
  167.    lcd.write((byte)0);
  168.    lcd.print("C");
  169.  }
  170.  else
  171.  {
  172.    lcd.print(f);
  173.    lcd.write((byte)0);
  174.    lcd.print("F");
  175.  }
  176.    
  177. }