#include <math.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include <BMP085.h>

RTC_DS1307 RTC;
BMP085 dps = BMP085();

const boolean DEBUG = true;
const int tempPin = 0;
const int tiltPin = 6;
const int chipSelect = 10;

int tiltState = 0;
long Temperature = 0, Pressure = 0, Altitude = 0;
unsigned long time1=0;

void setup() {
  Serial.begin(9600); // initialize serial
  RTC.begin();
  Wire.begin();
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(__DATE__, __TIME__));
    return;
  }
  dps.init(MODE_STANDARD, 1700, true);  // 17 meters initial altitude, true = using meter units
  if (DEBUG) { Serial.print("Initializing SD card..."); }
  if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
    if (DEBUG) { Serial.println("Card failed, or not present"); }
    return; // don't do anything more:
  }
  if (DEBUG) { Serial.println("card initialized."); }
  pinMode(10, OUTPUT);   // output, even if you don't use it:
  pinMode(tiltPin, INPUT); // initialize the tilt pin as an input:
}

void loop() {
  // open the file. note that only one file can be open at a time
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) { // if the file is available, write to it:
    DateTime now = RTC.now();
    dataFile.print(now.year(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.day(), DEC);
    dataFile.print(' ');
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(':');
    dataFile.print(now.second(), DEC);
    dataFile.print(',');
    if (DEBUG) {
      Serial.print("Date:");
      Serial.print(now.year(), DEC);
      Serial.print("/");
      Serial.print(now.month(), DEC);
      Serial.print("/");
      Serial.print(now.day(), DEC);
      Serial.print(" Time:");
      Serial.print(now.hour(), DEC);
      Serial.print(":");
      Serial.print(now.minute(), DEC);
      Serial.print(":");
      Serial.print(now.second(), DEC);
      Serial.print(" ExtTemp:");
    }
    float TT = readTemp();
    dataFile.print(TT);
    dataFile.print(",");
    if (DEBUG) {
      Serial.print(TT);
      Serial.print(" Tilt:");
    }
    tiltState = digitalRead(tiltPin);
    if (tiltState == HIGH) {
      dataFile.print("1,");
      if (DEBUG) { Serial.print("Yes "); }
    } 
    else {
      dataFile.print("0,");
      if (DEBUG) { Serial.print("No "); }
    }
    int sensorValue = analogRead(1);
    float Rsensor;
    Rsensor=(float)(1023-sensorValue)*10/sensorValue;
    dataFile.print(Rsensor);
    dataFile.print(',');
    if (DEBUG) {
      Serial.print(" Light:");
      Serial.print(Rsensor);
    }
    if (((millis() - time1)/1000.0) >= 1.0) {     
      dps.calcTrueTemperature();
      time1 = millis();      
    }
    dps.getTemperature(&Temperature); 
    dps.getPressure(&Pressure);
    dps.getAltitude(&Altitude);
    dataFile.print(Temperature);
    dataFile.print(',');
    dataFile.print(Altitude);
    dataFile.print(',');
    dataFile.println(Pressure);
    if (DEBUG) {
      Serial.print(" IntTemp:");
      Serial.print((double)Temperature/10);
      Serial.print(" Alt:");
      Serial.print(Altitude);
      Serial.print(" Press:");
      Serial.println(Pressure);
    }
  }
  else {
    Serial.println("Error opening file for writing");
  }
  dataFile.close();
  delay(1000);
}

float readTemp()
{
  const int B=3975; 
  double TEMP;
  int sensorValue = analogRead( tempPin );
  float Rsensor;
  Rsensor=(float)(1023-sensorValue)*10000/sensorValue;
  TEMP=1/(log(Rsensor/10000)/B+1/298.15)-273.15;
  return TEMP;
}