I am working up a sketch to log data for model rocket flights. I have a TinyZero with the accelerometer. I have the accelerometer working, plus temperature and milliseconds. I have an SD card module on order, and I need to work out the code to create a .CSV filename with sequential numbers. I dont much care about the numbers, so much as I care about not overwriting the data from the previous flight the next time the unit is powered on. And I want a separate CSV file for each flight.
Can anyone point me at a guide for how to program such a thing? Or better yet a sample of code from someones project where the accomplish the same thing?
Thanks.
//Model Rocket Data Logger - Built on TinyCircuits TinyZero with Accelerometer
//Accelerometer data in values of 'G', Temperature in 'C', Pressure, and time stored on SD card as a .CSV file
#include <Wire.h> // For I2C communication with sensor
#include "BMA250.h" // For interfacing with the accel. sensor
#include <SPI.h>
#include <SD.h>
// Accelerometer sensor variables for the sensor and its values
BMA250 accel_sensor;
int x, y, z, runtime;
int AccelX, AccelY, AccelZ;
double temp;
String dataString = "";
char filename[16];
const int chipSelect = 10;
//////////////////////////////////////Setup Section////////////////////////////////////////////////////////////////////////////
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
Wire.begin();
SD.begin(chipSelect);
dataString += " Milliseconds , Accel Z , Accel X , Accel Y , Temp , Pressure";
// Set up the BMA250 acccelerometer sensor
accel_sensor.begin(BMA250_range_16g, BMA250_update_time_64ms);
}
////////////////////////////////////Data Wrangling//////////////////////////////////////////////////////////////////////////////
void loop() {
accel_sensor.read();//This function gets new data from the acccelerometer
float x = accel_sensor.X;
float y = accel_sensor.Y;
float z = accel_sensor.Z;
temp = ((accel_sensor.rawTemp * 0.5) + 24.0);
float AccelX = (x/32);
float AccelY = (y/32);
float AccelZ = (z/32);
float runtime = millis();
//////////////////////////////////Output Section///////////////////////////////////////////////////////////////////////////////
SerialUSB.print(" Temperature(C) = "); SerialUSB.print(temp); SerialUSB.print(" Seconds: "); SerialUSB.println(runtime/100);
SerialUSB.print (" X Accel: "); SerialUSB.print(AccelX); SerialUSB.print (" Y Accel: "); SerialUSB.print(AccelY); SerialUSB.print (" Z Accel: "); SerialUSB.println(AccelZ);
}