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

TempLogger

By: a guest on Oct 8th, 2012  |  syntax: C  |  size: 2.96 KB  |  views: 83  |  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. // -----------------------------------------------------------------
  2.  
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5. #include <SPI.h>
  6. #include <SD.h>
  7.  
  8. // Data wire is plugged into pin 7 on the Arduino
  9. #define ONE_WIRE_BUS 7
  10.  
  11. // Setup a oneWire instance to communicate with any OneWire devices
  12. // (not just Maxim/Dallas temperature ICs)
  13. OneWire oneWire(ONE_WIRE_BUS);
  14.  
  15. // Pass our oneWire reference to Dallas Temperature.
  16. DallasTemperature sensors(&oneWire);
  17. float temperature = 0.0;
  18.  
  19. // -----------------------------------------------------------------
  20.  
  21. #include <LiquidCrystal.h>
  22.  
  23. // Connections:
  24. // rs (LCD pin 4) to Arduino pin 12
  25. // rw (LCD pin 5) to Arduino pin 11
  26. // enable (LCD pin 6) to Arduino pin 10
  27. // LCD pin 15 to Arduino pin 13
  28. // LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
  29. LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
  30.  
  31. int backLight = 13;    // pin 13 will control the backlight
  32.  
  33. // -----------------------------------------------------------------
  34.  
  35. //SD card
  36. const int sdPin = 9;
  37. File logFile;
  38.  
  39. // -----------------------------------------------------------------
  40.  
  41. void setup(void)
  42. {
  43.   // start serial port
  44.   Serial.begin(9600);
  45.   Serial.println("Dallas Temperature IC Control");
  46.   // Start up the library
  47.   sensors.begin();
  48.  
  49.   pinMode(backLight, OUTPUT);
  50.   digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  51.   lcd.begin(16,2);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  52.   lcd.clear();                  // start with a blank screen
  53.   lcd.setCursor(0,0);           // set cursor to column 0, row 0 (the first row)
  54.   lcd.print("Dallas Temperature IC Control");    // change this text to whatever you like. keep it clean.
  55.   lcd.setCursor(0,1);           // set cursor to column 0, row 1
  56.   lcd.print("DS18B20");
  57.   delay(1000);
  58.  
  59.   if(!SD.begin(sdPin)) {
  60.     Serial.println("Failed to initialize SD card");
  61. //    lcd.setCursor(0,0);
  62. //    lcd.println("Init SD failed  ");
  63.   } else {
  64.     Serial.println("SD initialized.");
  65. //    lcd.setCursor(0,0);
  66. //    lcd.println("Init SD success ");
  67.   }
  68.   delay(1000);
  69. }
  70.  
  71.  
  72. void loop(void)
  73. {
  74.   // call sensors.requestTemperatures() to issue a global temperature
  75.   // request to all devices on the bus
  76.   Serial.print(" Requesting temperatures...");
  77.   sensors.requestTemperatures(); // Send the command to get temperatures
  78.   Serial.println("DONE");
  79.   temperature = sensors.getTempCByIndex(0);
  80.  
  81.   Serial.print("Temperature for Device 1 is: ");
  82.   Serial.println(temperature); // Why "byIndex"?
  83.     // You can have more than one IC on the same bus.
  84.     // 0 refers to the first IC on the wire
  85.   lcd.setCursor(0,0);
  86.   lcd.print(temperature);
  87.   lcd.print("*C         ");
  88.   lcd.setCursor(0,1);
  89.   lcd.print(millis()/1000);
  90.   lcd.print(" sec                 ");
  91.  
  92.   logFile = SD.open("cookLog.txt", FILE_WRITE);
  93.   logFile.print(millis());
  94.   logFile.print("\t");
  95.   logFile.println(temperature);
  96.   logFile.close();
  97. }