Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

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