// RTC stuff
#include <RTClib.h>
RTC_DS1307 rtc; // Define class "RTC_DS1307" object named "rtc"
#define RTC_TYPE 1
char daysOfWeek[7][10] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char months[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
// Potentionmeter stuff
int pot;
int val;
// SD Card stuff
#include <SdFat.h>
#define SPI_SPEED SD_SCK_MHZ(4) // Up to 50. Reduce if errors occur.
#define CS_PIN 10
#define SD_FAT_TYPE 0
SdFat sd;
SdFile file;
char filename[14] = {"Data_Log.txt"};
String SdWrite = String('\0');
int SdWriteLength = 0;
char SdRead[64] = {0}; // Size chosen arbitrarily. Reduce size to increase performance, or increase size to reduce overflow.
void setup() { // put your setup code here, to run once:
// Start serial connection between Arduino board and PC:
Serial.begin(115200);
// Start RTC, and throw error if unsuccessful:
if (!rtc.begin()) {
Serial.println("RTC setup error!");
Serial.flush();
abort();
}
if (!rtc.isrunning()) {
Serial.println("RTC not running!");
}
// Start SD card, and throw error if unsuccessful:
if (!sd.begin(CS_PIN, SPI_SPEED)) {
if (sd.card()->errorCode()) {
Serial.println("SD card setup error!");
} else if (sd.vol()->fatType() == 0) {
Serial.println("SD card partition error!");
} else {
Serial.println("SD card setup error!");
}
Serial.flush();
abort();
}
// Setup success:
Serial.println("Setup success!");
// Show files on SD card:
Serial.println("Files on SD card:");
Serial.println(" Size Name");
sd.ls(LS_R | LS_SIZE);
}
void loop() { // put your main code here, to run repeatedly:
// Read RTC clock:
DateTime now = rtc.now();
Serial.print(daysOfWeek[now.dayOfTheWeek()]);
Serial.print(' ');
Serial.print(months[now.month()-1]);
Serial.print(' ');
if (now.day() < 10) {
Serial.print('0');
}
Serial.print(now.day(), DEC);
Serial.print(", ");
Serial.print(now.year(), DEC);
Serial.print(' ');
if (now.hour() < 10) {
Serial.print('0');
}
Serial.print(now.hour(), DEC);
Serial.print(':');
if (now.minute() < 10) {
Serial.print('0');
}
Serial.print(now.minute(), DEC);
Serial.print(':');
if (now.second() < 10) {
Serial.print('0');
}
Serial.print(now.second(), DEC);
Serial.println();
// Read potentiometer, and map value to a range of 0 to 100:
pot = analogRead(0);
val = map(pot, 0, 1023, 0, 100);
Serial.print("Potentiometer value: ");
Serial.print(val);
Serial.println();
// Write string to SD card:
SdWrite = "Time " + String(now.hour(),DEC) + ":" + String(now.minute(),DEC) + ":" + String(now.second(),DEC)
+ " Pot " + String(val,DEC);
SdWriteLength = SdWrite.length() + 2; // The "+2" accounts for new line and null characters (?) at the end
file.open(filename,O_RDWR|O_APPEND|O_CREAT);
file.println(SdWrite);
file.sync();
Serial.print("String written to SD card: ");
Serial.println(SdWrite);
Serial.print("Write length (bytes): ");
Serial.println(String(SdWriteLength,DEC));
Serial.print("File size (bytes): ");
Serial.println(file.fileSize());
// Read string back from SD card:
file.seekEnd(-1 * SdWriteLength); // Set the files position to the beginning of the last-written string
for (int i = 0; i <= 64; i++) {
SdRead[i] = '\0'; // Clear the "SdRead" char array buffer before using it again
}
file.read(SdRead,SdWriteLength-2); // The "-2" excludes the new line and null characters (?) at the end
Serial.print("String read back from SD card: ");
Serial.println(String(SdRead));
file.close();
// Wait until clock advances 1 second to continue:
while (now == rtc.now()) {}
// Add blank line to serial output:
Serial.println();
}