|
-
-
- #include <SPI.h>
- #include <SdFat.h>
- #include <SdFatUtil.h> // define FreeRam()
-
- #define SD_CHIP_SELECT SS
- #define USE_DS1307 0
- #define LOG_INTERVAL 1000
- #define SENSOR_COUNT 3
- #define ECHO_TO_SERIAL 1
- #define WAIT_TO_START 1
- #define ADC_DELAY 10
-
-
- SdFat sd;
-
-
- ofstream logfile;
-
-
- ArduinoOutStream cout(Serial);
-
-
- char buf[80];
-
- #if SENSOR_COUNT > 6
- #error SENSOR_COUNT too large
- #endif
-
-
- #define error(s) sd.errorHalt(F(s))
-
- #if USE_DS1307
-
-
-
-
-
-
- #error remove this line and uncomment the next two lines.
-
-
- RTC_DS1307 RTC;
-
-
- void dateTime(uint16_t* date, uint16_t* time) {
- DateTime now = RTC.now();
-
-
- *date = FAT_DATE(now.year(), now.month(), now.day());
-
-
- *time = FAT_TIME(now.hour(), now.minute(), now.second());
- }
-
-
- ostream& operator << (ostream& os, DateTime& dt) {
- os << dt.year() << '/' << int(dt.month()) << '/' << int(dt.day()) << ',';
- os << int(dt.hour()) << ':' << setfill('0') << setw(2) << int(dt.minute());
- os << ':' << setw(2) << int(dt.second()) << setfill(' ');
- return os;
- }
- #endif
-
- void setup() {
- Serial.begin(9600);
- while (!Serial) {}
-
-
- cout << endl << F("FreeRam: ") << FreeRam() << endl;
-
- #if WAIT_TO_START
- cout << F("Type any character to start\n");
- while (Serial.read() <= 0) {}
- delay(400);
- while (Serial.read() >= 0) {}
- #endif
-
- #if USE_DS1307
-
- Wire.begin();
- if (!RTC.begin()) {
- error("RTC failed");
- }
-
-
- SdFile::dateTimeCallback(dateTime);
- DateTime now = RTC.now();
- cout << now << endl;
- #endif
-
-
- if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {
- sd.initErrorHalt();
- }
-
-
- char name[] = "logger00.csv";
-
- for (uint8_t i = 0; i < 100; i++) {
- name[6] = i/10 + '0';
- name[7] = i%10 + '0';
- if (sd.exists(name)) {
- continue;
- }
- logfile.open(name);
- break;
- }
- if (!logfile.is_open()) {
- error("file.open");
- }
-
- cout << F("Logging to: ") << name << endl;
- cout << F("Type any character to stop\n\n");
-
-
- obufstream bout(buf, sizeof(buf));
-
- bout << F("millis");
-
- #if USE_DS1307
- bout << F(",date,time");
- #endif
-
- for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
- bout << F(",sens") << int(i);
- }
- logfile << buf << endl;
-
- #if ECHO_TO_SERIAL
- cout << buf << endl;
- #endif
- }
-
- void loop() {
- uint32_t m;
-
-
- do {
- m = millis();
- } while (m % LOG_INTERVAL);
-
-
- obufstream bout(buf, sizeof(buf));
-
-
- bout << m;
-
- #if USE_DS1307
- DateTime now = RTC.now();
- bout << ',' << now;
- #endif
-
-
- for (uint8_t ia = 0; ia < SENSOR_COUNT; ia++) {
- #if ADC_DELAY
- analogRead(ia);
- delay(ADC_DELAY);
- #endif
- bout << ',' << analogRead(ia);
- }
- bout << endl;
-
-
- logfile << buf << flush;
-
-
- if (!logfile) {
- error("write data failed");
- }
-
- #if ECHO_TO_SERIAL
- cout << buf;
- #endif
-
-
- if (m == millis()) {
- delay(1);
- }
-
- if (!Serial.available()) {
- return;
- }
- logfile.close();
- cout << F("Done!");
- while (1);
- }
|