Forked from raghavendrahassy/gist:3a85bce1b4aacb17246b71d0c1d3b96c
Last active
March 9, 2017 12:57
-
-
Save Xplorer001/6e80fb5db5355902ea2071a88c217def to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <SD.h> | |
#include <SparkFunDS1307RTC.h> | |
#include <Wire.h> | |
#include "max6675.h" | |
int ktcSO = 8; | |
int ktcCS = 9; | |
int ktcCLK = 10; | |
const int chipSelect = 4; | |
#define SQW_INPUT_PIN 2 // Input pin to read SQW | |
MAX6675 ktc(ktcCLK, ktcCS, ktcSO); | |
void setup() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
pinMode(SQW_INPUT_PIN, INPUT_PULLUP); | |
digitalRead(SQW_INPUT_PIN); | |
rtc.begin(); // Call rtc.begin() to initialize the library | |
// (Optional) Sets the SQW output to a 1Hz square wave. | |
// (Pull-up resistor is required to use the SQW pin.) | |
rtc.writeSQW(SQW_SQUARE_1); | |
// Now set the time... | |
// You can use the autoTime() function to set the RTC's clock and | |
// date to the compiliers predefined time. (It'll be a few seconds | |
// behind, but close!) | |
// rtc.autoTime(); | |
// Or you can use the rtc.setTime(s, m, h, day, date, month, year) | |
// function to explicitly set the time: | |
// e.g. 7:32:16 | Monday October 31, 2016: | |
//rtc.setTime(58, 33, 8, 4, 9, 3, 17); // Uncomment to manually set time | |
// rtc.set12Hour(); // Use rtc.set12Hour to set to 12-hour mode | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
Serial.print("Initializing SD card..."); | |
// 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."); | |
delay(500); | |
} | |
void loop() { | |
static int8_t lastSecond = -1; | |
// Call rtc.update() to update all rtc.seconds(), rtc.minutes(), | |
// etc. return functions. | |
rtc.update(); | |
if (rtc.second() != lastSecond) // If the second has changed | |
{ | |
printTime(); // Print the new time | |
lastSecond = rtc.second(); // Update lastSecond value | |
} | |
// Read the state of the SQW pin and show it on the | |
// pin 13 LED. (It should blink at 1Hz.) | |
digitalRead(SQW_INPUT_PIN); | |
// make a string for assembling the data to log: | |
String dataString = ""; | |
float sensor = ktc.readCelsius(); | |
// Serial.print(sensor); | |
dataString += String(sensor); | |
// open the file. note that only one file can be open at a time, | |
// so you have to close this one before opening another. | |
File dataFile = SD.open("datalog.csv", FILE_WRITE); | |
// if the file is available, write to it: | |
if (dataFile) { | |
dataFile.print(String(rtc.hour()) + ":"); // Print hour | |
dataFile.print(String(rtc.minute()) + ":"); // Print minute | |
dataFile.print(String(rtc.second())); // Print second | |
dataFile.print(";"); | |
dataFile.print(String(rtc.date()) + "/" + // (or) print date | |
String(rtc.month()) + "/"); // Print month | |
dataFile.print(String(rtc.year())); // Print year | |
dataFile.print(";"); | |
dataFile.print("Temprature;"); // Temprature | |
dataFile.print(dataString); | |
dataFile.println(";"); | |
dataFile.close(); | |
// print to the serial port too: | |
Serial.println(dataString); | |
delay(5000); | |
} | |
// if the file isn't open, pop up an error: | |
else { | |
Serial.println("error opening datalog.txt"); | |
} | |
} | |
void printTime() | |
{ | |
Serial.print(String(rtc.hour()) + ":"); // Print hour | |
if (rtc.minute() < 10) | |
Serial.print('0'); // Print leading '0' for minute | |
Serial.print(String(rtc.minute()) + ":"); // Print minute | |
if (rtc.second() < 10) | |
Serial.print('0'); // Print leading '0' for second | |
Serial.print(String(rtc.second())); // Print second | |
if (rtc.is12Hour()) // If we're in 12-hour mode | |
{ | |
// Use rtc.pm() to read the AM/PM state of the hour | |
if (rtc.pm()) Serial.print(" PM"); // Returns true if PM | |
else Serial.print(" AM"); | |
} | |
Serial.print(" | "); | |
// Few options for printing the day, pick one: | |
Serial.print(rtc.dayStr()); // Print day string | |
//Serial.print(rtc.dayC()); // Print day character | |
//Serial.print(rtc.day()); // Print day integer (1-7, Sun-Sat) | |
Serial.print(" - "); | |
#ifdef PRINT_USA_DATE | |
Serial.print(String(rtc.month()) + "/" + // Print month | |
String(rtc.date()) + "/"); // Print date | |
#else | |
Serial.print(String(rtc.date()) + "/" + // (or) print date | |
String(rtc.month()) + "/"); // Print month | |
#endif | |
Serial.println(String(rtc.year())); // Print year | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment