/*
  Listfiles

  This example shows how print out the files in a
  directory on a SD card

  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe
  modified 2 Feb 2014
  by Scott Fitzgerald

  This example code is in the public domain.

*/
#include <SPI.h>
#include <SD.h>
#include "HX711.h"

int delta_t = 100; // time between samples in ms (10 hZ)

HX711 strain;


String filename = ""; //filename is set by user input during setup
File root;

unsigned long start_time = 0;
unsigned long last_write_time = 0;



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

  Serial.println("Initializing the scale");
  strain.begin(A1, A0);

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

  if (!SD.begin(10)) { // use SDCARD_SS_PIN for actual device
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
  Serial.println("enter file name: ");

  boolean recievedFileName = false;
  while (!recievedFileName){
    if (Serial.available() > 0) { 
      char incomingChar = Serial.read();
      if (incomingChar == '\n'){
        recievedFileName = true;
      }
      else {
        filename = filename + incomingChar;
      }
    }
  }

  Serial.println(filename);
  delay(1000);

  start_time = millis();
}

void loop() {
  if (millis() - last_write_time >= delta_t){
    last_write_time = millis();
  
    // make a string for assembling the data to log:
    String dataString = (String)(millis()-start_time);
    dataString += ",";

    dataString += strain.get_units();
    // // read three sensors and append to the string:
    // for (int analogPin = 0; analogPin < 3; analogPin++) {
    //   int sensor = analogRead(analogPin);
    //   dataString += String(sensor);
    //   if (analogPin < 2) {
    //     dataString += ",";
    //   }
    // }

    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    File dataFile = SD.open(filename, FILE_WRITE);

    // if the file is available, write to it:
    if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
      // print to the serial port too:
      Serial.println(dataString);
    }
    // if the file isn't open, pop up an error:
    else {
      Serial.println("error opening datalog.txt");
    }
  }
}

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();
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
sd1:CD
sd1:DO
sd1:GND
sd1:SCK
sd1:VCC
sd1:DI
sd1:CS
hx711:VCC
hx711:DT
hx711:SCK
hx711:GND