SD Card communication issue?

Hi, here my code. I have a futek TRS605 torque sensor with an encoder built-in with and arduino UNO R3 with an external SD card module. I'm trying to record a torque value at each 3 deg. and create a table a bit like this:

Torque , angle
15.0 , 3
16.0 , 6
18.0 , 9
[...] etc.

when I'm done with this recording, I switch Off a rocker switch to turn off the data recording and then it would write this table in a data1.txt file on the SD card. then if I switch back on the recording, another table it created and then exported to another file named data2.txt.

But when I try to do it, here's what my serial monitor gives me:

Card initialized.

0 ,, 0 ,, 0

void loop

1 ,, 0 ,, 0
-------> here I switch to HIGH the pin 7 (recordpin) from a rocker switch
confirmSTARTrec

Recording started...

recTABLEdata()


3.00 deg , torque:470.65
no index :1
----------------
----------------
6.00 deg , torque:484.49
no index :2
----------------
----------------
9.00 deg , torque:456.81
no index :3
----------------
-----> *** here I switch to LOW the pin 7 (recordpin) from the same rocker switch***

st

-----> //*** the st come from the serial.print(stoprecETsavedata) and it freeze here with nothing on my SD card***

Please help!

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

const int torquePin = A0;  // Pin for torque signal
const int angle1Pin = 2;   // Pin for angle signal 1
const int angle2Pin = 3;   // Pin for angle signal 2
const int recordPin = 7;   // Pin to start/stop recording
const int recordLED = 6;   // LED de confirmation pour enregistrement
const int OnLED = 5;       // LED de confirmation que tout est ready

/*
---SD card pin plug---
MOSI - pin 11 on Arduino Uno
MISO - pin 12 on Arduino Uno
SCK - pin 13 on Arduino Uno
CS - pin 4
*/


int fileIndex = 1;
bool isRecording = false;
bool carteSDok = false;
File dataFile;
bool memoryfull = false;

int bouttonrecord = 0;

float torque = 0.00;
float angle = 0;
int pulse1 = digitalRead(angle1Pin);
int pulse2 = pulse1;
int resolutionangle = 0;

struct Measurement {
  float torque;
  float angle;
};

Measurement measurements[80];  // Adjust size as needed
int measurementIndex = 0;

float readtorque() {
  float torquevolt = analogRead(torquePin) * (5.00 / 1023.00);
  float torquevalue = torquevolt * 20 * 141.61;
  return torquevalue;
}


//-----SETUP----------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(torquePin, INPUT);
  pinMode(angle1Pin, INPUT);
  pinMode(angle2Pin, INPUT);
  pinMode(recordPin, INPUT_PULLUP);
  pinMode(recordLED, OUTPUT);
  pinMode(OnLED, OUTPUT);

  digitalWrite(recordLED, LOW);
  digitalWrite(OnLED, LOW);
  pulse1 = digitalRead(angle1Pin);
  pulse2 = pulse1;

  while (!SD.begin(4)) {
    delay(500);
    Serial.println("Insérer Carte SD");
  }

  Serial.println("Card initialized.");
  digitalWrite(OnLED, HIGH);
  carteSDok = true;
}

//-------MAIN LOOP--------------------------------------------------------------------------------
void loop() {
  if (carteSDok) {
    delay(500);

    bouttonrecord = digitalRead(recordPin);  // vérifie bouttonrec= 1 ou 0

    Serial.print(bouttonrecord);
    Serial.print("  ,, ");
    Serial.print(isRecording);
    Serial.print("  ,, ");
    Serial.println(memoryfull);


    if (bouttonrecord == HIGH && !isRecording && !memoryfull) {  //    bouttonrec= 1 ,, isRecording= 0 ,, memoryfull= 0
      confirmSTARTrec();                                         //switch isrecording --> 1
      delay(50);
      recTABLEdata();  //switch memoryfull --> 1 ou 0
    }

    if (bouttonrecord == LOW && isRecording && !memoryfull) {  //    bouttonrec= 0 ,, isRecording= 1 ,, memoryfull= 0
      stoprecETsavedata();                                     //switch isrecording --> 0
    }

    if (bouttonrecord == HIGH && isRecording && memoryfull) {  //    bouttonrec= 1 ,, isRecording= 1 ,, memoryfull= 1
      while (bouttonrecord == 1) {                             // doit switcher bouttonrec --> 0 manuellement
        digitalWrite(recordLED, LOW);
        delay(2000);
        digitalWrite(recordLED, HIGH);
        delay(100);
        digitalWrite(recordLED, LOW);
        delay(100);
        digitalWrite(recordLED, HIGH);
        delay(100);
        digitalWrite(recordLED, LOW);
        delay(100);
        digitalWrite(recordLED, HIGH);
        delay(100);
        bouttonrecord = digitalRead(recordPin);
        if (bouttonrecord == 0) {
          stoprecETsavedata();  //switch isrecording --> 0
          memoryfull = false;   //switch memoryfull --> 0
        }
      }
    }

    if (!SD.begin(4)) {
      handleSDCardRemoval();
    }

    Serial.println("void loop");
  }
}



//---Fonctions-----------------------------------------------------------------------------------
void confirmSTARTrec() {  //    bouttonrec= N/A ,, isRecording--> 1 ,, memoryfull= N/A
  Serial.println("confirmSTARTrec");
  isRecording = true;
  digitalWrite(recordLED, HIGH);
  Serial.println("Recording started...");
  measurementIndex = 0;
  angle = 0;
}


void stoprecETsavedata() {  //    bouttonrec= N/A ,, isRecording--> 0 ,, memoryfull= N/A
  Serial.println("stoprecETsavedata");
  isRecording = false;
  digitalWrite(recordLED, LOW);
  Serial.println("Recording stopped.");
  delay(20);
  saveDataToSD();
}


void recTABLEdata() {  //    bouttonrec= N/A ,, isRecording= N/A ,, memoryfull--> 1 ou 0
  Serial.println("recTABLEdata()");
  bouttonrecord = digitalRead(recordPin);
  bool save = false;

  while (bouttonrecord == HIGH) {
    bouttonrecord = digitalRead(recordPin);
    pulse1 = digitalRead(angle1Pin);
    if (measurementIndex < 80) {
      if (pulse1 != pulse2 && !save) {
        angle = angle + 0.5;
        pulse2 = pulse1;
        resolutionangle++;
        if (resolutionangle == 6) {
          save = true;
        }
      }
      if (save) {
        torque = readtorque();
        measurements[measurementIndex].angle = angle;
        measurements[measurementIndex].torque = torque;
        measurementIndex++;
        Serial.println("----------------");
        Serial.print(angle);
        Serial.print(" deg , torque:");
        Serial.println(torque);
        Serial.print("no index :");
        Serial.println(measurementIndex);
        Serial.println("----------------");
        save = false;
        resolutionangle = 0;
        delay(50);
      }
    } else {
      Serial.println("Memory full");
      memoryfull = true;
      break;
    }
  }
}

void handleSDCardRemoval() {
  Serial.println("handleSDCardRemoval()");
  digitalWrite(OnLED, LOW);
  carteSDok = false;
  while (!SD.begin(4)) {
    Serial.println("Carte retirée, problème SD");
    delay(500);
    while (bouttonrecord == 1) {
      digitalWrite(recordLED, LOW);
      delay(300);
      digitalWrite(recordLED, HIGH);
      delay(300);
      bouttonrecord = digitalRead(recordPin);
      Serial.println("Turn off recording");
    }
  }
  Serial.println("Card ré-initialisée, tout est ok");
  digitalWrite(OnLED, HIGH);
  carteSDok = true;
}


void saveDataToSD() {  // à l'intérieur du void stoprecETsavedata()
  Serial.println("saveDataToSD()");
  Serial.print("dataFile pre:");
  Serial.println(dataFile);
  delay(20);
  char fileName[13];
  sprintf(fileName, "data%d.txt", fileIndex);
  Serial.print("fileName: ");
  Serial.println(fileName);
  delay(300);
  dataFile = SD.open(fileName, FILE_WRITE);
  delay(300);
  Serial.print("datafile post: ");
  Serial.println(dataFile);

  if (dataFile) {
    Serial.println("SD en écriture...");
    dataFile.println(fileName);
    dataFile.println("Torque (lb*po) , Angle (deg)");
    for (int i = 0; i < measurementIndex; i++) {
      dataFile.print(measurements[i].torque);
      dataFile.print(" , ");
      dataFile.println(measurements[i].angle);
    }
    dataFile.close();
    delay(100);
    fileIndex++;
    Serial.println("Data saved to SD card. fichier: " + String(fileName));
  } else {
    Serial.println("Error opening file.");
  }
}




It looks like your problem is on lines 120, 121 and 122:

 if (!SD.begin(4)) {
 handleSDCardRemoval();
 }

You are initializing an SD that has already been initialized in lines 64 to 66.

while (!SD.begin(4)) {
 delay(500);
 Serial.println("Insert SD Card");
 }

Comment on lines 120 to 122 and see if it works correctly.

If you want to test whether the SD is initialized, see if there is any function in the library that does this without re-initializing the SD.

Or use a flag when begin at setup().

hey it seems to work!! the only issue now is that the "dataFile" seems to be at 0 so it won't go in the writing process (the "if (dataFile) {" condition ) at the line 231

any idea?

void saveDataToSD() {  // à l'intérieur du void stoprecETsavedata()
  Serial.println("saveDataToSD");
  Serial.print("dataFile pre:");
  Serial.println(dataFile);
  delay(20);
  char fileName[13];
  sprintf(fileName, "data%d.txt", fileIndex);
  Serial.print("fileName: ");
  Serial.println(fileName);
  delay(300);
  dataFile = SD.open(fileName, FILE_WRITE);
  delay(300);
  Serial.print("datafile post: ");
  Serial.println(dataFile);

  if (dataFile) {
    Serial.println("SD en écriture...");
    dataFile.println(fileName);
    dataFile.println("Torque (lb*po) , Angle (deg)");
    for (int i = 0; i < measurementIndex; i++) {
      dataFile.print(measurements[i].torque);
      dataFile.print(" , ");
      dataFile.println(measurements[i].angle);
    }
    dataFile.close();
    delay(100);
    fileIndex++;
    Serial.println("Data saved to SD card. fichier: " + String(fileName));
  } else {
    Serial.println("Error opening file.");
  }
}