#include <SdFat.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <Time.h>
#include <DS1307RTC.h>
// SD chip select pin. Be sure to disable any other SPI devices such as Enet.
const uint8_t chipSelect = SS;
Adafruit_ADS1115 ads;
int16_t results;
tmElements_t tm;
const uint32_t SAMPLE_INTERVAL_MS = 1;
int detik,menit,jam;
#define FILE_BASE_NAME "DATA"
//------------------------------------------------------------------------------
// File system object.
SdFat sd;
// Log file.
SdFile file;
//==============================================================================
// User functions. Edit writeHeader() and logData() for your requirements.
//------------------------------------------------------------------------------
// Write data header.
void writeHeader() {
if (RTC.read(tm)) {
file.print("time ");
file.print(tm.Hour);
file.write(':');
file.print(tm.Minute);
file.write(':');
file.print(tm.Second);
file.println();
}
}
//------------------------------------------------------------------------------
// Log a data record.
void logData(){
file.print(results);
file.println();
}
//==============================================================================
// Error messages stored in flash.
#define error(msg) error_P(PSTR(msg))
//------------------------------------------------------------------------------
void error_P(const char* msg) {
sd.errorHalt_P(msg);
}
//------------------------------------------------------------------------------
void setup() {
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.CSV";
Serial.begin(115200);
delay(10);
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
// Find an unused file name.
if (BASE_NAME_SIZE > 6) {
error("FILE_BASE_NAME too long");
}
while (sd.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
error("Can't create file name");
}
}
if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) error("file.open");
Serial.print(F("Logging to: "));
Serial.println(fileName);
ads.setGain(GAIN_TWO);
ads.begin();
// Write data header.
writeHeader();
if (RTC.read(tm)) {
jam = tm.Hour;
menit = tm.Minute;
detik = tm.Second;
}
}
//------------------------------------------------------------------------------
void loop() {
results = ads.readADC_Differential_0_1();
logData();
// Force data to SD and update the directory entry to avoid data loss.
if (!file.sync() || file.getWriteError()) error("write error");
if (RTC.read(tm)) {
int H = tm.Hour;
int M = tm.Minute;
int D = tm.Second;
if(H==00) H=24;
if (H == (jam+1) && M==menit && D == detik ) {
file.print("time :");
file.print(tm.Hour);
file.write(':');
file.print(tm.Minute);
file.write(':');
file.print(tm.Second);
file.println();
// Close file and stop.
file.close();
Serial.println(F("Done"));
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.CSV";
// Find an unused file name.
if (BASE_NAME_SIZE > 6) {
error("FILE_BASE_NAME too long");
}
while (sd.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
error("Can't create file name");
}
}
file.open(fileName, O_CREAT | O_WRITE | O_EXCL);
Serial.print(F("Logging to: "));
Serial.println(fileName);
// Write data header.
writeHeader();
jam = tm.Hour;
menit = tm.Minute;
detik = tm.Second;
}
}
}
Comments