#include <Wire.h>
//#include <VL53L0X.h>
#include <SPI.h>
#include <SD.h>
// Create VL53L0X object with name sensor
File logFile; // Declare variable "textfile" as File (dts-file)
const int chipSelect = 5; // SD-Card at PIN_5 (CS-Port)
unsigned long myTime; // Variable to store the time in ms since the program started
int distance = 0;
// Uncomment this line to use long range mode. This
// increases the sensitivity of the sensor and extends its
// potential range, but increases the likelihood of getting
// an inaccurate reading because of reflections from objects
// other than the intended target. It works best in dark
// conditions.
//#define LONG_RANGE
// Uncomment ONE of these two lines to get
// - higher speed at the cost of lower accuracy OR
// - higher accuracy at the cost of lower speed
#define HIGH_SPEED
//#define HIGH_ACCURACY
void setup() {
Serial.begin(9600);
//------------------------------------------Setup VL53L0X------------------------------------------
//------------------------------------------Setup SD-Card Reader------------------------------------------
Serial.println("Initialise SD-Card");
if (!SD.begin(chipSelect)) { // If SD-Card can't be found (!SD.begin(5)=Data from PIN_5)...
Serial.println("Initialisation failed!"); // ... an error will occur...
return;
}
Serial.println("Initialisation completed!"); // ... otherwise the message "Initialisation completed!" will occur.
char filename[] = "Log_00.txt";
for (uint8_t i = 0; i < 100; i++) {
filename[4] = i/10 + '0';
filename[5] = i%10 + '1';
if (!SD.exists(filename) == false) { // Check if file exists
logFile = SD.open(filename, FILE_WRITE); // Open a new file with new name.
Serial.println("-----New file created-----");
Serial.print("filename: ");
Serial.println(filename);
logFile.close(); // Close file
break; // Leave loop
}
else
{
continue;
}
}
}
void loop() {
delay(1000);
//------------------------------------------Loop VL53L0X------------------------------------------
myTime = millis(); // Get the time in ms
distance = analogRead(0); // Get the distance from the sensor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" mm ");
Serial.print("Time: ");
Serial.print(myTime);
Serial.print(" ms");
Serial.println();
//---------------------------------------Loop SD-Card Reader---------------------------------------
String dataString = ""; // Create a string for assembling the data to log.
dataString += String(myTime); // Append string with time in ms
dataString += ","; // Append string with separator (",")
dataString += String(distance); // Append string with distance
}