// Adafruit
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;
// ------------------------------ //

// RTC
#include "RTClib.h"

RTC_DS1307 rtc;
// ------------------------------ //


//Thermistor
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// ------------------------------ //

//SD Card
// #include "SdFat.h"
#include <SD.h>

// #define SPI_SPEED SD_SCK_MHZ(4)
#define chipSelect 53

File root;

// SdFat sd;
// ------------------------------ //


void setup(void) {
  Serial.begin(9600);
  
  mpu_setup();
  rtc_setup();
  SD_setup();

  File writeFile = SD.open("wokwi.txt", FILE_WRITE);
  writeFile.write("Testing microSD card write");
  writeFile.close();
  
  // Example of reading file from the card:
  File textFile = SD.open("wokwi.txt");
  if (textFile) {
    Serial.print("wokwi.txt: ");
    while (textFile.available()) {
      Serial.write(textFile.read());
    }
    textFile.close();
  } else {
    Serial.println("error opening wokwi.txt!");
  }
   
}

sensors_event_t event;

void loop() {
// Accelerometer
  mpu.getAccelerometerSensor()->getEvent(&event);
  float X_out = event.acceleration.x;
  float Y_out = event.acceleration.y;
  float Z_out = event.acceleration.z;

// RTC  
  DateTime now = rtc.now();

  int hour = now.hour();
  int minute = now.minute();
  int second = now.second();

// Temperature
  float temp = temperature();

  Serial.print(millis());
  Serial.print(",");
  Serial.print(hour, DEC);
  Serial.print(",");
  Serial.print(minute, DEC);
  Serial.print(",");
  Serial.print(second, DEC);
  Serial.print(",");
  Serial.print(X_out);
  Serial.print(",");
  Serial.print(Y_out);
  Serial.print(",");
  Serial.print(Z_out);
  Serial.print(",");
  Serial.print(temp);
  Serial.println("");

  // make a string for assembling the data to log:
  String dataString = "";
  dataString = String(millis())+","+String(hour)+","+String(minute)+","+String(second)+","+String(X_out)+","+String(Y_out)+","+String(Z_out)+","+String(temp);

  delay(500);
}

void mpu_setup(){
  if (!mpu.begin(0x69)) {
    Serial.println("Couldn't find MPU6050");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 ready!");  
}

void rtc_setup(){
    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  } 
  Serial.println("RTC ready!");    
}

void SD_setup(){
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

  Serial.println("Files in the card:");
  root = SD.open("/");
  printDirectory(root, 0);
  Serial.println("");  
}

float temperature(){
  // Temperature  
  int analogValue = analogRead(A0);
  float temp = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;  
  return temp;
}

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}
GND5VSDASCLSQWRTCDS1307+