ENVshield SD card storage issue

Hello everyone! I am attempting to use an Arduino MKR1010 and ENV shield to capture data on an SD card. The sketch in this post verifies and uploads onto the board however, I do not get a file created on the SD card that stores the sensor data. Also, this sketch makes the board constantly connect and disconnect from the computer. What am I doing wrong?

/* 
  Sketch generated by the Arduino IoT Cloud Thing "GreenDuino"
  https://create.arduino.cc/cloud/things/c9febcef-b462-4371-a588-28531c050e36 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float uvb;
  float uvi;
  float pressure;
  float uva;
  float temperature;
  float lux;
  float humidity;
  float soilMoisture;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <Arduino_MKRENV.h>
#include <ArduinoOTA.h>
#include <SPI.h>
#include <SD.h>


// variables for the hygrometer
const int AirValue = 570;
const int WaterValue = 0;
const int SD_CS_PIN = 4;

int intervals = (AirValue - WaterValue)/3;
int soilMoistureValue = 0;
// PIN SETUPS
int pinOutRelay = 13;

//file object
File dataFile;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  
  // PIN SETUPS
  pinMode(pinOutRelay, OUTPUT);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  //
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  if (!ENV.begin()) {
    Serial.println("Failed to initialize MKR ENV shield!");
    while(1);
  }
  
  //init SPI
  SPI.begin();
  delay(100);
  
  //init SD card
  if(!SD.begin(SD_CS_PIN)) {
    Serial.println("Failed to initialize SD card!");
    while (1);
  }
  
  //init the logfile
  File datafile = SD.open("log-0000.csv", FILE_WRITE);
  delay(1000);
  
  //init the CSV file with headers
  dataFile.println("temperature,humidity,pressure,UVA,UVB,UVindex");
  
  //close the file
  datafile.close();
  delay(100);
  
}
  //
void loop() {
  
  //init cloud
  ArduinoCloud.update();
  
  //init the logfile
  dataFile = SD.open("log-0000.csv", FILE_WRITE);
  
  humidity = ENV.readHumidity();
  lux = ENV.readIlluminance();
  pressure = ENV.readPressure();
  temperature = ENV.readTemperature();
  uva = ENV.readUVA();
  uvb = ENV.readUVB();
  uvi = ENV.readUVIndex();
  
  // print each of the sensor values
  dataFile.print(temperature);
  dataFile.print(",");
  dataFile.print(humidity);
  dataFile.print(",");
  dataFile.print(pressure);
  dataFile.print(",");
  dataFile.print(uva);
  dataFile.print(",");
  dataFile.print(uvb);
  dataFile.print(",");
  dataFile.println(uvi);
  
  //close the file
  dataFile.close();
  
  //wait 1 sec and print again
  delay(1000);
  
  //delay(1000);
    soilMoistureValue = analogRead(A0);
    if(soilMoistureValue < 190)
    {
      Serial.println("Very Wet");
      digitalWrite(pinOutRelay, LOW);
    }
    else if(soilMoistureValue < 380)
    {
      Serial.println("Wet");
    }
    else if(soilMoistureValue < 890)
    {
      Serial.println("Dry");
      digitalWrite(pinOutRelay, HIGH);
    }
    delay(10000);
    Serial.println(soilMoistureValue);
    
    soilMoisture = soilMoistureValue;
}

void onHumidityChange() {
  // Do something
}
void onLuxChange() {
  // Do something
}
void onPressureChange() {
  // Do something
} 
void onTemperatureChange() {
  // Do something
}
void onUvaChange() {
  // Do something
}
void onUvbChange() {
  // Do something
}
void onUviChange() {
  // Do something
}
void onMoistureChange() {
  // Do something
}
void onmoistureChange() {
  // Do something
}
void onSoilMoistureChange() {
  // Do something
}

@gr33nduino, please edit your opening post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

The above might be the cause of your problem; you do the same in loop() as well. You open a file, if it fails you still try to write to it and you close it. Closing a file (file handle) that is not open can result in crashes.

Edited the post, thank you! I will take a deeper look into that snippet and play around with it. My goal is to have it read out to the IoT cloud dashboard as well as capture the data there. I am automating an heirloom tomato bed. Next would be to measure soil PH levels and capture that data as well. Thank you!

@sterretje I have messed with the code a little but still am stumped. Do I need to have the file already created on the SD card? I thought this was supposed to create the file and write to it.

It should create the file.

I don't have experience with the MKR family and no MKR boards so can only help theoretically. If I was stuck, I would start with one of the SD examples and see if that works.

Note:
When you modify code, you should post your latest version.