// simple data logger // // using Arduino Leonardo and an Adafruit Logger Shield // // uses Adafruit's RTClib // // Based on https://github.com/cnvogelg/ardu/blob/master/datalog/datalog/datalog.ino #include #include #include #include #include #include #include "Bounce.h" // Pin setup const int ledGreenPin = 8; const int ledRedPin = 4; const int buttonPin = 6; const int oneWirePin = 7; const int chipSelect = 10; #define DEBOUNCE 10 #define BUFSIZE 32 // ----- GLOBALS ----- RTC_DS1307 RTC; OneWire oneWire(oneWirePin); DallasTemperature sensors(&oneWire); uint16_t delta = 10; // measurement delta in seconds uint32_t next_ts; static uint16_t year; static uint8_t month, day, hour, minute, second; Bounce logToggleButton; File file; // offsets: 0123456789012 byte name[] = "yymmdd_x.log"; // ----- FUNCTIONS ----- static void led(int pin, int on) { digitalWrite(pin, on ? HIGH : LOW); } // fatal error - blink led and wait for reset static void fail() { while(1) { led(ledRedPin, 1); delay(200); led(ledRedPin, 0); delay(200); } } static int sd_init() { pinMode(chipSelect, OUTPUT); //if (!SD.begin(chipSelect)) { if (!SD.begin(10,11,12,13)) { Serial.println("SD init failed"); fail(); } } static void rtc_init() { Wire.begin(); RTC.begin(); if (! RTC.isrunning()) { Serial.println("RTC is NOT running!"); } // get value for first measurement next_ts = RTC.now().unixtime(); } static uint16_t parse_dec(byte *buf, byte num) { uint16_t val = 0; for(byte i=0;i= '0')&&(buf[i] <= '9')) { byte v = buf[i] - '0'; val += v; } else { return 0xffff; } } return val; } static void to_dec(uint16_t val, byte *buf, byte num) { byte *ptr = buf + num - 1; for(byte i=0;i= next_ts) { led(ledGreenPin, 1); next_ts += delta; // do measurement sensors.requestTemperatures(); float t = sensors.getTempCByIndex(0); char temp[BUFSIZE]; // format: t:1= String line = ""; line += ts; line += " t:1="; dtostrf(t, 5, 2, temp); line += temp; Serial.println(line); if (file) { file.println(line); file.flush(); } led(ledGreenPin, 0); } }