Weird problem with SD card (it works in one program and it does not in another)

Hello everybody and thank you in advance,

I am making a project in which i was using two sketches of code until now. In the one I was using my SD card and I was creating and putting in files serial data from a sensor. In the other , I was taking data from the sensor and I was implementing Fast Fourier Transform via arduinoFFT library. Everything was going fine until I decided to merge these codes so that I can store the data of the sensor to a file inside the SD card while I was watching in my serial plot the fourier transformation of the same data. ( I need to do that in order to take the data and implement fft on them through matlab so that I can be sure that my method is right and the resulted plots of fft are the same.) THEN , this happened. When i uploaded the code it got stuck in my subroutine for the initialization of the SD card and showed in the serial monitor the first letters again and again. “InInInIn” because I have a Serialprintln “Initialization in Process” . It doesnt return neither false nor true. Just prints the first letters of my subroutine command. I REALLY donnot know what to do and I have VERY little time in my hands. I would be very grateful if you could enlighten me or give me some advice.

I have a jpg attached.

How much RAM does the compiler report you have used in the combined sketch?

Sketch uses 17314 bytes (53%) of program storage space. Maximum is 32256 bytes.
Global variables use 1167 bytes (56%) of dynamic memory, leaving 881 bytes for local variables. Maximum is 2048 bytes.
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\Users\RAMONI~1\AppData\Local\Temp\arduino_build_33327/analogreadval.ino.hex:i

Thanks for devoting time !!!

Also I have put some messages betweend codes and I need you to see this new jpg here.

you don't need to set the SD CS pin to OUTPUT, but you should set it as parameter to SD.begin (if it is not pin 4)

show the sketch

Juraj:
you don’t need to set the SD CS pin to OUTPUT, but you should set it as parameter to SD.begin (if it is not pin 4)

show the sketch

#include <SPI.h>
#include <SD.h>
#include <arduinoFFT.h>
#define SAMPLES 128 // prepei na einai dynamh tou 2
#define SAMPLING_FREQUENCY 1000 // prepei na einai mikroterh tou 10.000 logw ADC

arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period;
unsigned long microseconds;
const float corrector = 0.048;
float val= 0.000;
float accel=0.000;
const int CS_PIN = 10;
const int ACC_PIN = A0;
File file;
void setup() {
Serial.begin(9600);
sampling_period = round(1000000 * ( 1.00/SAMPLING_FREQUENCY)) ;
Serial.println(“ok”);
initializeSD();
Serial.println(“ok2”);
delay(1);
createFile(“testfourier.txt”);
closeFile();
delay(1);
}

void loop() {
//delay(15000); //delay 15 sec gia peiramatikous skopous
double freal[SAMPLES];
double fimag[SAMPLES];
openFile(“testfourier.txt”);

for (int i=0; i<SAMPLES; i++) {
microseconds= micros(); // epistrefei ton ari8mo se microseconds ap otan to arduino arxise na ektelei to programma
char buffer[16];
val=analogRead(ACC_PIN);
delay(1);
accel=val * corrector; //metatroph V se epitaxynsh g
//Serial.println(accel);
dtostrf(accel,5,3,buffer);//5 ta synolika pshfia & 3 to dekadiko meros
freal*=accel;*
fimag*=0;*
writeToFile(buffer);
while ( micros() < (microseconds + sampling_period)){} ;
}
FFT.Windowing(freal, SAMPLES , FFT_WIN_TYP_HAMMING , FFT_FORWARD );
FFT.Compute(freal, fimag, SAMPLES , FFT_FORWARD);
FFT.ComplexToMagnitude(freal,fimag,SAMPLES);
//double peak= FFT.MajorPeak(freal, SAMPLES, SAMPLING_FREQUENCY);
//Serial.println (peak); //printaroume thn megalyterh energeiaka syxnothta
for (int i = 0; i<(SAMPLES/2); i++) {
Serial.print((i* 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
* Serial.print(" ");*
_ Serial.println(freal*, 1);
}
closeFile();
delay(1);
while (1); //run code once
}
//SUBROUTINES APO KATW*_

void initializeSD() {
* Serial.println(“Initializing SD card…”);*
* Serial.println(“ok1.5”);*
* pinMode(CS_PIN, OUTPUT);
_ if (SD.begin())
{
Serial.println(“SD card is ready to use.”);
} else*
* {
Serial.println(“SD card initialization failed”);
return;
}
}
int createFile(char filename[])
{_
file = SD.open(filename, FILE_WRITE);
_ if (file)
{
Serial.println(“File created successfully.”);
return 1;
} else*
* {
Serial.println(“Error while creating file.”);
return 0;
}
}
int writeToFile(char text[])
{
if (file)
{
file.println(text);
Serial.println("Writing to file: ");
Serial.println(text);
return 1;
} else*
* {
Serial.println(“Couldn’t write to file”);
return 0;
}
}
void closeFile()
{
if (file)
{
file.close();
Serial.println(“File closed”);
}
}
int openFile(char filename[])
{_
file = SD.open(filename,FILE_WRITE);
_ if (file)
{
Serial.println(“File opened with success!”);
return 1;
} else*
* {
Serial.println(“Error opening file…”);
return 0;
}
}*_

default pin in SD library is 4. you use pin 10? tell it with SD.begin(CS_PIN);

Juraj:
default pin in SD library is 4.
you use pin 10?
tell it with SD.begin(CS_PIN);

But it works in another code. And it does create a file in which I have put data from the sensor…

Also see in the picture below… I did us you said and the same thing happened in Serial Monitor when I uploaded the code.