Problem Saving to SD card in Main Loop

Hello all, i am currently experiencing an issue trying to save information to an SD card i have attached with a shield. The outcome would be to capture how many times a servo motor runs at a certain part within the main loop. i keep retrieving a fail at the SD Initialization on the serial monitor as if it the FILE_WRITE function fails to even work! Any help would be appreciated. I inserted a small block if the code below containing the trouble areas and the full version of the code in the attachment.

Thanks alot!!!

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


String readString;

int Distance_to_Preheat;
int Preheat_Dwell;
int Distance_to_Dip;
int Dwell_Time;
int Distance_to_Raise;
int Spin_Time;

int Upper_Limit = A0;
int Lower_Limit = A1;
int E_Stop = 2;
int Return = 3;
int Start = 5;
int Spindle_EN = 6;
int Step_Direction = 8;
int Step_Speed = 10;

int Debug = 3000;
int Step_Counter = 0;
int E_Stop_State = 0;
int Return_State = 0;
int Start_State = 0;
int Up = 0;
int Down = 0;
int Preheat = 0;
int Dip = 0;
int Raise = 0;
int Spin = 0;
int Home = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    }

  Serial.print("Initializing SD card...");
  
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
    }    
  
  Serial.println("initialization done.");

  
  } //End Setup

void loop() {

  if(Serial.available() > 0) { 
      
    while (Serial.available()) {
      delay(10);
        
      if(Serial.available() > 0) {
        char commandInChar = Serial.read();
        readString += commandInChar;
        }
      }
    
    }
  

 

   ///Start of Spin Motor and SD Read/Write

  if(Spin == 1){
       
    digitalWrite(Spindle_EN, HIGH);
    delay(1);
    Step_Counter = Step_Counter + 1;    
  
    if(Step_Counter >= Spin_Time || digitalRead(E_Stop) == LOW || Return_State == HIGH ){
            
      digitalWrite(Spindle_EN, LOW);
      Step_Counter = 0;
      Spin = 0; 
      Home = 1;     

      if(digitalRead(E_Stop) == LOW || Return_State == HIGH ){
        Spin = 0;
        Home = 0;
        }       
      }
      Serial.print("i H8 LYFE");  
      DataLogs();
      Serial.print("i lUV LYFE"); 
    }



   ///End of Spin Motor and SD Read/Write

   
  } //End Void Loop



void DataLogs() {
 File myFile  = SD.open("TEST.TXT", FILE_WRITE);
  if (myFile) {
    myFile.println("Spindle Activity Enabled/Detected");
    myFile.close();
    } 
    else {
      Serial.println("error opening TEST.TXT");
      }
 
  myFile = SD.open("TEST.TXT");
  
  if (myFile) {
    Serial.println("TEST.TXT: content");
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    myFile.close();
    } 
    else {
      Serial.println("error opening TEST.TXT");
      }
  }//End DataLogs*/

AGR_v2.1.ino (7.81 KB)

I suspect you have done a sloppy job of combining two programmes, thereby getting a lot of redundant stuff - and adding that to other redundant stuff. Your file subroutine need be no more than

 myFile = SD.open(filename, FILE_WRITE);
  myFile.print(mystuff);
  etc,
  myFile.close();