#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 7

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);


#define CS_PIN 10
float Temperatura;
 int i;
unsigned long myTime = 0;
unsigned long previusTime = 0;
unsigned long Time_meas = 0;
unsigned long myTime1 = 0;

File file;
int myArray[20];
void setup() {
  Serial.begin(9600);
  sensors.begin();
   Serial.print("Initializing SD card... ");

  if (!SD.begin(CS_PIN)) {
    Serial.println("Card initialization failed!");
    while (true);
  }

  Serial.println("initialization done.");


  file = SD.open("acqu.csv", FILE_WRITE); //File in scrittura

  file.println("nuova acquisizione - ");
  file.print("time");
  file.print(",");
  file.println("temperatura");

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

  }
 

   
void loop() {
  // This will ensure we take the temperature every second
  myTime1 = millis();
  if (myTime1 - Time_meas >= 1000) {
    // Request temperature from the sensor
    sensors.requestTemperatures();
    Temperatura = sensors.getTempCByIndex(0);  // Get temperature from the first sensor (index 0)
    
    // Store the temperature reading in the array
    myArray[i] = Temperatura;

    // Print the current temperature from the array
    Serial.print(" | ");
    Serial.print(myArray[i]);
    Serial.print (" | ");
    Serial.print(i);
    Serial.println(" - Temperature stored");

   

    // Update the time of last measurement
    Time_meas = myTime1;

    // Increment the index, making sure it doesn't exceed the array size
    i++;
    
    // Reset the index when it reaches the end of the array (after storing 10 values)
    if (i >= 20) {

file = SD.open("acqu.csv", FILE_WRITE); //File in scrittura
    

  for (int i = 0 ;i <20; i++){
   file.print(myArray[i]);
   
  file.print(",");
  file.println("temperatura");
  Serial.println(myArray[i]);
  
  }

  file.close(); 


     i = 0;  // Reset index to 0 to overwrite the array from the beginning
    }
 
  }
 
 
 
 
 /* //  da provare con reale hardware
  File file = SD.open("acqu.vsd");
  if (file) {
    Serial.print("acqu.vsd ");
    while (file.available()) {
      Serial.write(file.read());
    }
   file.close();
  } else {
    Serial.println("error opening wokwi.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();
  }
}