Laser to SD card

I’m trying to get the vl53l0x laser to print the distance measurement to an SD card. I can not see where I am going wrong.
The sd card is does initialise when the code is run.
I am able to get the measurement to print to serial however, when i change “serial.println” to “Damper.println” no data is wrote in the SD card

All help is greatly appreciated

#include <SD.h> //Load SD library
#include <SPI.h>
#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

int CS_PIN = 4; //chip select pin for the MicroSD Card Adapter
File Damper; // file object that is used to read and write data

void setup() {
  Serial.begin(115200); // start serial connection to print out debug messages and data

  pinMode(CS_PIN, OUTPUT); // chip select pin must be set to OUTPUT mode
  if (!SD.begin(CS_PIN)) { // Initialize SD card
    Serial.println("Could not initialise SD card."); // if return value is false, something went wrong.
  }

  if (SD.exists("Damper.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("Damper.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while (1);
  }
  // power
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}

void loop() {
  VL53L0X_RangingMeasurementData_t measure;

  Damper = SD.open("Damper.txt", FILE_WRITE); // open "file.txt" to write data

  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (Damper) {

    if (measure.RangeStatus != 4) {  // phase failures have incorrect data
      Damper.println(measure.RangeMilliMeter);
    } else {
      Damper.println(" out of range ");
    }
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  delay(1000);
}

Try getting the CardInfo program working first.

I made wrote a code which prints to the SD card successfully and used that as a base trying to include the laser library into the code.

#include <SPI.h>
#include <SD.h>

File Damper;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  while (!Serial) {
    delay(1); // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");
  if (!SD.begin(6)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
}

void loop() {
  Damper = SD.open("Damper.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (Damper) {
       Damper.println(7);

   // close the file:
    Damper.close();
      } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

Quite correct the important bit is that you MUST close the file. Closing the file completes a lot of behind the scenes stuff that is critical for you to be able to read the file.

Whats happening is basically the same as if you ejected a SD card from your camera while it was still saving a photo... You can see the file is there, but empty.

Until you have output 512 bytes to the file, nothing gets written, as SDcard blocksize is 512 bytes. Learn to use file.flush() and file.close().