Saving Data to SD when button pressed

Hello together,
i want to save my acc-data to SD-Card
therefor i have 2 buttons:
first button to choose Measurement-style
second button for starting and ending measurement and saving data to sd...

#include "SparkFunLSM6DS3.h"
#include <SPI.h>
#include <SdFat.h>
#include <LiquidCrystal.h>
#include <Wire.h> // Must include Wire library for I2C

// ################################################################ LCD
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 11, 12, 13);

// SD chip select pin. Be sure to disable any other SPI devices such as Enet.
const uint8_t chipSelect = SS;

// Log file base name. Must be six characters or less.
#define FILE_BASE_NAME "LOGGER"
//------------------------------------------------------------------------------
// File system object.
SdFat sd;

// Log file.
SdFile file;

// Time in micros for next data record.
uint32_t logTime;

const uint8_t ANALOG_COUNT = 10;

// ################################################################ Gyro ACC Sensor
LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B
//// Begin using the library by creating an instance of the MMA8452Q
//MMA8452Q accel;
float ax;
float ay;
float az;
float gx;
float gy;
float gz;
float gT;

float axmax;
float aymax;
float azmax;
float vvmax;

float vv;

// ################################################################ Taster
// Taster einbinden
const int TastMessReihe = 5;
unsigned long bsTMR = 0;
int TMR = 0;
const int TastSS = 6;
unsigned long bsTSS = 0;
int TSS = 0;
//const int TasterStart = 4;

int vMR = 0; // Zähler für Messreihe
int vSS = 0; // Zähler für Messung gestartet

unsigned long MilliStart;
unsigned long MilliMess;
unsigned long MilliEnd;
unsigned long vDelay;

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ writeHeader
// Write data header.
void writeHeader() {
file.print("Startzeit: ");
file.print(gT);
file.print(" - Geraete-Temperatur: ");
file.print(gT);
file.print(" °C");
file.println();
file.print("Zeit; a_x; a_y; a_z; w_x; w_y; w_z; v; Loc_X; Loc_Y");
file.println();
file.print("ms; ms2; ms2; ms2; Grad/s; Grad/s; Grad/s; km/h; -; -");

file.println();
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ logData
// Log a data record.
void logData() {

file.print(MilliMess);
file.print("; ");
file.print(ax);
file.print("; ");
file.print(ay);
file.print("; ");
file.print(az);
file.print("; ");
file.print(gx);
file.print("; ");
file.print(gy);
file.print("; ");
file.print(gz);
file.print("; ");
file.print(MilliMess);
file.print("; ");
file.print(MilliMess);
file.print("; ");
file.print(MilliMess);
file.print("; ");

file.println();
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ writeHeader KURZ
// Write data header.
void writeHeaderKurz() {
file.print("Startzeit: ");
file.print(gT);
file.print(" - Geraete-Temperatur: ");
file.print(gT);
file.print(" °C");
file.println();
file.print("Zeit; a_x; a_y; a_z; v");
file.println();
file.print("ms; ms2; ms2; ms2; km/h");

file.println();
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ logData KURZ
// Log a data record.
void logDataKurz() {

file.print(MilliMess);
file.print("; ");
file.print(ax);
file.print("; ");
file.print(ay);
file.print("; ");
file.print(az);
file.print("; ");
file.print(gx); //// V eintragen

file.println();
}
//==============================================================================
// Error messages stored in flash.
#define error(msg) sd.errorHalt(F(msg))

void AnzeigeNormal()
{
lcd.setCursor(0, 0);
lcd.print("X:");
if (ax < 0){
lcd.print(ax,2);
}
else
{
lcd.print("+");
lcd.print(ax,2);
}
lcd.setCursor(7, 0);
lcd.print(" Z:");
if (az < 0){
lcd.print(az,2);
}
else
{
lcd.print("+");
lcd.print(az,2);
}

// Cursor auf zweite Zeile, 0. Spalte bewegen
lcd.setCursor(0, 1);
lcd.print("Y:");
if (ay < 0){
lcd.print(ay,2);
}
else
{
lcd.print("+");
lcd.print(ay,2);
}
lcd.setCursor(7, 1);
lcd.print(" v:");
// lcd.print(vV,2);
}

void AnzeigeMax()
{
lcd.setCursor(0, 0);
lcd.print("X:");
lcd.print(axmax, 2);

lcd.setCursor(7, 0);
lcd.print(" Z:");
lcd.print(azmax, 2);

lcd.setCursor(0, 1);
lcd.print("Y:");
lcd.print(aymax, 2);

lcd.setCursor(7, 1);
lcd.print(" v:");
lcd.print(vvmax, 2);
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ S E T U P
void setup() {
// put your setup code here, to run once:
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.csv";

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

// ################################################################ Gyro-Accel
myIMU.begin();
Wire.begin();

pinMode(TastMessReihe, INPUT);
pinMode(TastSS, INPUT);

// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
// Print a message to the LCD.
lcd.print("Los geht s");

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ L O O P
void loop() {
// put your main code here, to run repeatedly:

MilliStart = millis();
MilliMess = millis();

// ################################################################ Beschleunigungsmesser

ax=myIMU.readFloatAccelX() * 9.81;
ay=myIMU.readFloatAccelY() * 9.81;
az=myIMU.readFloatAccelZ() * 9.81;

// ################################################################ Taster
bsTMR = digitalRead(TastMessReihe);
bsTSS = digitalRead(TastSS);

/////////////////////////////////////////////////////////////////////////////////
if ((bsTMR == HIGH) && (TMR == 0) && (vSS == 0)){ // Taster Messung Button MeasurementStyle

TMR = 1;
vMR ++;
if (vMR == 3) //Derzeit 3 Messungen möglich
{
vMR = 0;
}
}
if ((bsTMR == LOW) && (TMR == 1)) {
TMR = 0;
}
/////////////////////////////////////////////////////////////////////////////////////
if ((bsTSS == HIGH) && (TSS == 0) && (vMR > 0)) // Taster Startstop Button Measurement Start Stop
{

TSS = 1;
vSS++;

if (vSS == 1) // Messung gestartet
{
// create a new file
Serial.println("Messung gestartet");
axmax = 0;
aymax = 0;
azmax = 0;
vvmax = 0;
StartMess();
//////////////// What Doing HERE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

if (vSS == 2) // Messung beendet
{
// Close file
Serial.println("Messung gestoppt");
}

if (vSS == 3)
{
Serial.println("Messung genullt");
vSS = 0;
}

}
if ((bsTSS == LOW) && (TSS == 1))
{
TSS = 0;
}
//////////////////////////////////////////////////////////////////////////////////////////// Ende TasterAbfrage

//////////////////////////////////////////////////////////////////////////////////////////// Max Werte berechnen
if (vSS == 1)
{
axmax = max(axmax, abs(ax));
aymax = max(aymax, abs(ay));
azmax = max(azmax, abs(az));
vvmax = max(vvmax, abs(vv));
}

//////////////////////////////////////////////////////////////////////////////////////////// Anzeige Einstellungen
if ((vMR == 0) && (vSS == 0)) //Anzeige
{
AnzeigeNormal();
}

if (vSS == 1)
{
AnzeigeMax();
}

if ((vMR == 1) && (vSS == 0)) //M1
{
lcd.setCursor(0, 0);
lcd.print("M1: Messung 1");
lcd.setCursor(0, 1);
lcd.print("50 Hz");
FILE_BASE_NAME == "Mess1_";
}
if ((vMR == 2) && (vSS == 0)) //M2
{
lcd.setCursor(0, 0);
lcd.print("M2: Messung 2");
lcd.setCursor(0, 1);
lcd.print("1 Hz");
FILE_BASE_NAME == "Mess2_";
}

}

Thanks for Help!
DC

Nice, but next time use the code box.. Anyway, what is your question?

How do I start (and end) a file to save data in void loop

Don't poll the button state with full loop speed, check the state with ~100 ms interval instead, or use debouncing circuit to eliminate fast random transitions.
Button event: I prefer if event is firing when button is down because this is more logical. Button-up means device is ready to listen for the next (button-down) event.

The button is not the Problem...

if ((bsTMR == HIGH) && (TMR == 0) && (vSS == 0)){ // Taster Messung Button MeasurementStyle

bsTMR is the ButtonState: Pressed or not
TMR is lift to 1 after the first ButtonDown and goes back to 0 when ButtonUp. and
(vSS is index if saving is running)

With this code everything is fine so that the if-loop is only done once.

But i can´t start a data-saving in this if-loop that is in the void -loop

So, how do i start a data-saving in the void loop??

DC

You need to init the sd first:

sd.begin(chipSelect, SD_SCK_MHZ(50)); // try a lower speed if SPI errors occur

then you can open/create the file:

file.open(fileName, FILE_WRITE);

When the measuring is complete close the file:

file.close();

But why don't you look into the SdFat examples? There is a dataLogger.ino sketch with all these functions...