Starting data logger with switch

Hello,

I am using Fatlib16's AnalogBinLogger to record a piezo attached to analog 0 at 8 kHz which i am doing successfully.

I would like to initiate and stop recording (sending data to SD card) by using a push button to allow the device to be portable. I will then convert the bin to csv file post recording.

I currently have the push button attached to digital pin 2.

I have edited the following sections to the code:

// Pin definitions.
//
// Digital pin to indicate an error, set to -1 if not used.
// The led blinks for fatal errors. The led goes on solid for SD write
// overrun errors and logging continues.
const int8_t ERROR_LED_PIN = -1;

// SD chip select pin.
const uint8_t SD_CS_PIN = 10; // Needs to be set to pin 10 for adafruit datalogger.

// Defining switch's to initiate and end recording.
const int8_t recordPin = 2;
int8_t recordVal;
void setup(void) {
  if (ERROR_LED_PIN >= 0) {
    pinMode(ERROR_LED_PIN, OUTPUT);
  }
  Serial.begin(9600);

  //Set digital pin 2 as input
  pinMode(recordPin, INPUT); 

  // Read the first sample pin to init the ADC.
  analogRead(PIN_LIST[0]);

  Serial.print(F("FreeStack: "));
  Serial.println(FreeStack());

  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) {
    sd.initErrorPrint();
    fatalBlink();
  }
}
//------------------------------------------------------------------------------
void loop(void) {
  // Read any Serial data.
  do {
    delay(10);
  } while (Serial.available() && Serial.read() >= 0);
  Serial.println();
  Serial.println(F("type:"));
  Serial.println(F("c - convert file to csv"));
  Serial.println(F("d - dump data to Serial"));
  Serial.println(F("e - overrun error details"));
  Serial.println(F("Press Switch - record ADC data"));


  while(!Serial.available()) {
    SysCall::yield();
  }
  char c = tolower(Serial.read());
  if (ERROR_LED_PIN >= 0) {
    digitalWrite(ERROR_LED_PIN, LOW);
  }
 // Read any Serial data.
 do {
    delay(10);
  } while (Serial.available() && Serial.read() >= 0);

  
  recordVal = digitalRead(recordPin);
if (recordVal == HIGH) {
    logData(); // Change to 1st digital switch for operating code(
  } else if (c == 'c') {
    binaryToCsv(); //Change to 2nd digital switch
  } else if (c == 'd') {
    dumpData();
  } else if (c == 'e') {
    checkOverrun(); 
  } else {
    Serial.println(F("Invalid entry"));
  }
  }
#else  // __AVR__
#error This program is only for AVR.
#endif  // __AVR__

Any help would be really appreciated, thanks.

After having a look around i've managed to find a solution.

void setup(void) {
 //Set digital pin 2 and 4 as inputs and on board LED as output.
  pinMode(startPin, INPUT); 
  pinMode(stopPin, INPUT);
  pinMode(recordingLED, OUTPUT);

  // set inititial LED state
  digitalWrite(recordingLED, LOW);
  
  if (ERROR_LED_PIN >= 0) {
    pinMode(ERROR_LED_PIN, OUTPUT);
  }
  Serial.begin(9600);
  
  // Read the first sample pin to init the ADC.
  analogRead(PIN_LIST[0]);

  Serial.print(F("FreeStack: "));
  Serial.println(FreeStack());

  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) {
    sd.initErrorPrint();
    fatalBlink();
  }
}
//------------------------------------------------------------------------------
void loop(void) {
 // discard any input
  while (Serial.read() >= 0) {}
  Serial.println(); 
  Serial.println(F("Press Start Button to record ADC data"));
  Serial.println(F("Press Stop Button to stop recording ADC data"));
  Serial.println(F("d - dump data to Serial"));  
  Serial.println(F("e - overrun error details"));

 while(!Serial.available() && !(digitalRead(startPin) == HIGH)) {}
  char c;
  if (digitalRead(startPin) == HIGH)
  {
    c = 'r';
  } else {
    c = tolower(Serial.read());
  }
  if (ERROR_LED_PIN >= 0) {
    digitalWrite(ERROR_LED_PIN, LOW);
  }
  if (c == 'c') {
    binaryToCsv();
  } else if (c == 'd') {
    dumpData();
  } else if (c == 'e') {    
    checkOverrun();
  } else if ((c == 'r') ) {
    digitalWrite(recordingLED, HIGH);
    logData();
    binaryToCsv();// Automatically converts bin file to CSV
    digitalWrite(recordingLED, LOW);
  } else {
    Serial.println(F("Invalid entry"));
  }
  
}