I'm using these includes as it's a data logger that writes to SD card and displays the data on a LCD and storing some variables in the EEPROM
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <EEPROMex.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <Adafruit_ADS1015.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
This is part of the setup
setSyncProvider(RTC.get); // the function to get the time from the RTC
lcd.setCursor(0, 0);
if (timeStatus() != timeSet)
lcd.print("Unable to sync with the RTC");
else
lcd.print("RTC has set the system time");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Initializing SD card...");
pinMode(CS_pin, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(CS_pin)) {
lcd.setCursor(0, 1);
lcd.print("Card failed, or not present");
while (1); //return;
}
lcd.print("card initialized.");
//char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
while (SD.exists(filename)) {
n++;
snprintf(filename, sizeof(filename), "data%03d.txt", n);
}
File dataFile = SD.open(filename, FILE_READ);
dataFile.close();
//now filename[] contains the name of a file that doesn't exist
//Write Log File Header
//Write Log File Header
dataFile = SD.open(filename, FILE_WRITE);
String header = " LOGGER,Time,Volts1(Volts),Amps(Amps),Serial Number";
dataFile.print(n);
dataFile.println(header);
read_data();
delay(100);
Vresults3 = Vresults0 / (R2 / (volts_scale + R2)); /// This will be removed and replaced only for testing
Vresults4 = Vresults1 * amps_scale ; //this is wrong 3.75V shows 37.52amps *100 shows 375.2amps
char timeStamp[26];
sprintf(timeStamp, "%2d,%02d/%02d/20%02d %02d:%02d:%02d,",
CS, day(), month(), (year() - 2000), hour(), minute(), second());
dataFile.print(timeStamp);
dataFile.print(Vresults3);
dataFile.print(",");
dataFile.print(Vresults4);
dataFile.println(",020002287");
dataFile.close();
CS = 2;
//Read the Configuration information (COMMANDS.txt)
File commandFile = SD.open("COMMANDS.txt");
if (commandFile)
{
unsigned long decade = pow(10, (commandFile.available() - 1));
while (commandFile.available())
{
unsigned long temp = (commandFile.read() - '0');
refresh_rate = temp * decade + refresh_rate;
decade = decade / 10;
}
}
else
{
lcd.setCursor(0, 3);
lcd.print("Could not read command file.");
return;
}
lcd.clear();
}