Efficient code for sd card speed

Hello!
my code which writes senor data along with time and valve status to sd card at a speed of 150 ms between each pressure reading. Due to this, I am missing several data points. Can anyone please have a look at it and suggest me how to make the code more efficient to improve the speed of data writing to SD card or any other method to increase writing speed to sd card?

One thing that might save some time would be to call getPressure once and save it in a variable and write that to the SD and to the serial monitor instead of calling getPressure over and over while you're writing. Really, don't you want to log the reading that you used to do that other math anyway?

Other things to look at:

  1. You appear to have a debounced button press test in the loop(). Try eliminating that to see if that has an impact on the performance.

  2. You appear to open and close the SD card file for every write operation. Maybe batch these and close and reopen the file periodically to minimise the risk of data loss.

You have a lot of Serial output in your sketch. You can speed that up.

//Serial.begin(9600)
Serial.begin(115200);

Perhaps some of the serial output is for debugging purposes and can be commented out, or enabled with a #ifdef debug toggle for debug output. Toggling Debug Code - Bugs & Suggestions - Arduino Forum

I want to log the reading which was used to do the math and am unable to do so

What math? There is no single reading to do any calculations.

Basically, you take two reading of pressure each pass through loop() and assign the average value to senval[2].
You then use these values to compare with previous readings to determine max and min and peaks.

The value assigned to Pressure at the beginning of loop() which is logged is an independent reading.

Perhaps you want to be logging senval[2]? Try

void loop()
{
  float Pressure;
  //time = millis();
  Serial.print("\tprogramstarttime\t");
  Serial.println(time);
  //Pressure = getPressure();

  if (count == 0)
  {
    senval[1] = getPressure();//pressure_mmhg;

    senval[1] = (senval[1] + getPressure()) / 2;
    ++count;
  }

  else
  {
    senval[2] = getPressure();//pressure_mmhg;

    senval[2] = (senval[2] + getPressure()) / 2;
    Pressure = senval[2];
    time = millis(); // move to time of measurement
    Serial.print("\t senval[0]\t");
    Serial.println(senval[0]);
    Serial.print("\t senval[1]\t");
    Serial.println(senval[1]);
    Serial.print("\t senval[2]\t");
    Serial.println(senval[2]);

How to batch the sd card code to reopen file periodically?

Do not open and close the file each pass of the loop. The SD library actually buffers 512 bytes of the data and physically writes to the card when the buffer is full. Open the file in setup, leave it open. You can close the file when the test is complete (if you know when that is) or close and reopen the file on a timer(perhaps every minute or so, depending on how long your tests last.

Another possibility top speed things up is to switch from the SD library to SdFat.h which is faster.

Perhaps you want to be logging senval[2]? Try

I tried logging senval[2] and it works now!

Another possibility top speed things up is to switch from the SD library to SdFat.h which is faster.

I also replaced sd with sd fat library.

Thank you so much for the suggestions!