Printing to SD card within if statement

Hoping someone can help me with some programming for an Arduino Mega that I am using for my Master's project. I am very new to Arduino so please be nice!

I am trying to print sensor data to an SD card only when the sensor readings are not zero. This works fine for the serial monitor but doesn't seem to work for the SD card - it either prints all readings including zero readings, or prints nothing at all. The code below represents what I'm trying to do.

if (SensorValue > 0){

Serial.println(SensorValue);
delay(1000);

// Print to SD card file
TestData.println(SensorValue);

TestData.close();

}

If serial print and SD print are in the same if loop then the SD print includes all values i.e. zero and non-zero.

If they are in separate loops then nothing is printed to the SD.

Hope this I've explained this clear enough :slight_smile:

where do you open the file? (TestData)
how is SensorValue acquired? is that from an interrupt ?

--> don't post snippets

Apologies; below is my full code. In short what I am trying to do is only save to the file when any of the FSR readings are greater than zero i.e. save nothing when these are all zero.

#include <SPI.h>
#include <SD.h>

const int CS = 53;

#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(CS)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

  
}

int sample = 0;

void loop(){

  int FSR1 = analogRead(A0);
  int FSR2 = analogRead(A1);
  int FSR3 = analogRead(A2);
  int FSR4 = analogRead(A3);
  int POT = analogRead(A4);
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

  File TestData = SD.open("TestData.txt", FILE_WRITE);

  //if (TestData) {
    
    if ((FSR1 > 0) || (FSR2 > 0) || (FSR3 > 0) || (FSR4 > 0)){

      Serial.print(AcX);
      Serial.print("\t");
      Serial.print(AcY);
      Serial.print("\t");
      Serial.print(AcZ);
      Serial.print("\t");
      Serial.print(FSR1);
      Serial.print("\t");
      Serial.print(FSR2);
      Serial.print("\t");
      Serial.print(FSR3);
      Serial.print("\t");
      Serial.print(FSR4);
      Serial.print("\t");
      Serial.println(POT);
    
  

  // Print to SD card file
      TestData.print(sample);
      TestData.print("\t");
      TestData.print(AcX);
      TestData.print("\t");
      TestData.print(AcY);
      TestData.print("\t");
      TestData.print(AcZ);
      TestData.print("\t");
      TestData.print(FSR1);
      TestData.print("\t");
      TestData.print(FSR2);
      TestData.print("\t");
      TestData.print(FSR3);
      TestData.print("\t");
      TestData.print(FSR4);
      TestData.print("\t");
      TestData.println(POT);
      TestData.close();
      sample = sample + 1;
      delay(10);

    }
      
  
}

I did a bit of surgery to your code, see below. Nothing major, just took all the wire stuff out and I only have one potentioneter to test with. Also it seems not to go to 0 grrrr so I gave it some leeway and only wrote if >100 not >0. Also limited it to 100 samples. (I changed it so all readings are now numbered… easier to see that some were rejected.)

It works for me, as you can see from the data, it only wrote some of the samples.

Definitely no issue about doing a write to an SD inside an if, not that I expected there to be :wink:

#include <SPI.h>
#include <SD.h>

const int CS = 4;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(CS)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

}

int sample = 0;

void loop() {

  File TestData = SD.open("TestData.txt", FILE_WRITE);

  while (sample < 50)
  {
    int FSR1 = analogRead(A0);
    Serial.println(FSR1);
    if ((FSR1 > 100) )
    {
      Serial.println("writing to sd");
      // Print to SD card file
      TestData.print(sample);
      TestData.print("\t");
      TestData.println(FSR1);

    }
    else
    {
      Serial.println("      reading was zero");
    }
    sample = sample + 1;
    delay(1000);
  }

  TestData.close();
  Serial.println("finished");

}
5 328
6 582
7 582
8 442
9 447
10 494
14 315
15 577
16 454
18 271
19 572
20 572

Amazing thanks neiklot this has worked perfectly!

samfreem1:
Amazing thanks neiklot this has worked perfectly!

Excellent. Now you need to backtrack to your original code to see what’s different. Maybe taking my version as known-good, add back the stuff I removed piece by piece, and test it frequently so you know what would have caused the problem if the problem comes back.)