/* SD card datalogger */ #include #include #include "RTClib.h" #include "Adafruit_MAX31855.h" #define DP 1 // No. decimal places for serial output RTC_DS1307 rtc; //call library for clock //assign variables for max31855 const int chipSelect = 10; const int thermoCLK = 3; const int thermoCS = 4; const int thermoDO = 5; Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO); // initialize the thermocouple void setup() { Serial.begin(57600); // Open serial communications and wait for port to open: #ifdef AVR Wire.begin(); #endif rtc.begin(); if (! rtc.isrunning()) { Serial.println("RTC is NOT running!"); // following line sets the RTC to the date & time this sketch was compiled // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } Serial.print("Initializing SD card..."); // make sure that the default chip select pin is set to output, even if you don't use it: pinMode(10, OUTPUT); // see if the card is present and can be initialized: if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); // don't do anything more: return; } Serial.println("card initialized."); Serial.println("MAX31855 test"); // wait for MAX chip to stabilize delay(1000); } void loop(){ DateTime now = rtc.now(); //print date & time to serial Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); // basic readout test, just print the current temp Serial.print("Internal Temp = "); Serial.println(thermocouple.readInternal()); doOver: //reset to here if tc reading isnan float f = thermocouple.readFarenheit(); //read tc and store in variable if (isnan(f)) { Serial.println("Something wrong with thermocouple!"); Serial.print("errorBit = "); //read error bit and write to serial Serial.println(thermocouple.readError()); delay(1000); //wait a second, then read tc again goto doOver; } else { Serial.print("F = "); Serial.println(f); File dataFile = SD.open("datalog.txt", FILE_WRITE); // open the file if (dataFile) { // if the file is available, write to it: DateTime now = rtc.now(); //variable to store data/time //write date & time to SD card dataFile.print(now.year(), DEC); dataFile.print('/'); dataFile.print(now.month(), DEC); dataFile.print('/'); dataFile.print(now.day(), DEC); dataFile.print(' '); dataFile.print(now.hour(), DEC); dataFile.print(':'); dataFile.print(now.minute(), DEC); dataFile.print(':'); dataFile.print(now.second(), DEC); dataFile.println(); //wirte temp to SD card dataFile.print("F = "); dataFile.println(f); dataFile.close(); // print to the serial port too: Serial.println(); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.txt"); } delay(300000); //wait 5 min before next read/write } }