SD.begin(chipSelect);

This command is giving me many problems and I dont know if its because I’m doing something wrong.

The point is that I have a system with one arduino UNO and one Arduino SD card shield v4.1. I make some measurments with analog sensors and then I want to store the data in the SD card. The headache come when I want to initialize the sd card to store the data.

For testing I have nothing plugged to the sensors, only the sd card shield with and sd card inside and the pc to arduino wire.

If i remove the code of the sd card initialization (SD.begin(chipSelect); ) things works perfectly until that point. But when i put that code strange thing starts to happen.

This is the code I ripped from the sketch

#define V 500
#include <SD.h>
const int chipSelect = 4;

short curva[V];
short auxiliar[3];

int AnalogPin = 0;		//pin analogico 0
int LedPin = 13;		//pin con led
int Delay = 1000000;            //delay 200 microsegundos
int threshold = 205;            //umbral de disparo. No es float porque va de 0 a 1023
int n1 = 50;                    //datos guardados antes del trigger
int n2 = 450;                   //datos guardados despues del trigger
int c=0;

void setup()
{
 Serial.begin(9600);
 Serial.println("setup");
  
}


void loop() 
{
  delay(1000);
  Serial.println("loop");
  long int last = 0;
  long int now = 0;
  last = millis();

  for(int i=1; i<V; i++) 
  {             
    Serial.println("for");
    now = millis();
    curva[i] = analogRead(AnalogPin);
    if(i=1)
      {
        curva[0]=curva[1];
      }
    int diff=curva[i-1]-curva[i];
    delay(1000);
    Serial.print("Diff=");
    Serial.println(diff);
    if (diff > threshold)
      {
        int triggern = i;
        auxiliar[1]=triggern;
        int n3=triggern-n1;
        auxiliar[3]=n3;
        if(triggern < n1)
          {
          Serial.println("trigger");
          }
      }
    while(now == last) 
      {      // wait until 1 ms elapses
      delayMicroseconds(Delay);
      now = millis();
      }
    writelink2();
  }

  writelink1();
}



void writelink1() 
{
  Serial.println("writelink1");
}

void writelink2() 
{
  
  Serial.println("writelink2");
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
    pinMode(10, OUTPUT);
    /*
  // see if the card is present and can be initialized:
  SD.begin(chipSelect);
    
  Serial.println("card initialized.");
  
  File dataFile = SD.open("test.txt", FILE_WRITE);
  
  if (dataFile) 
  {
    dataFile.println("testing 1, 2, 3.");
    dataFile.close();
    // print to the serial port too:
    
  }  
  // if the file isn't open, pop up an error:
  else 
  {
    Serial.println("error opening curva1.txt");
  } 
  
*/
  while (true) {}
}

With the code above (notice the code removed due to /* */ ) The serial monitor shows the messages properly until that point. But if I run it with all the code I see strange messages in the serial monitor like:

setup
loop
for
DZ®W5
loop
for
DZ®W5

I tried to use the code from SD Readwrite example to make the sketch write the data.

Thanks

You should call SD.begin() only once in the setup() function.

SurferTim:
You should call SD.begin() only once in the setup() function.

I tried that but if i add the sd.begin() to the setup() function what happens is that the function setup() repeats forever and never starts loop()

This is the code

#define V 500
#include <SD.h>

const int chipSelect = 4;

short curva[V];
short auxiliar[3];

int AnalogPin = 0;		//pin analogico 0
int LedPin = 13;		//pin con led
int Delay = 1000000;            //delay 200 microsegundos
int threshold = 205;            //umbral de disparo. No es float porque va de 0 a 1023
int n1 = 50;                    //datos guardados antes del trigger
int n2 = 450;                   //datos guardados despues del trigger
int c=0;

void setup()
{
 Serial.begin(9600);
 Serial.println("setup");
 pinMode(10, OUTPUT);
 //SD.begin(chipSelect);
 
   if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while(true) {}
  }
  
}


void loop() 
{
  delay(1000);
  Serial.println("loop");
  long int last = 0;
  long int now = 0;
  last = millis();

  for(int i=1; i<V; i++) 
  {             
    Serial.println("for");
    now = millis();
    curva[i] = analogRead(AnalogPin);
    if(i=1)
      {
        curva[0]=curva[1];
      }
    int diff=curva[i-1]-curva[i];
    delay(1000);
    Serial.print("Diff=");
    Serial.println(diff);
    if (diff > threshold)
      {
        int triggern = i;
        auxiliar[1]=triggern;
        int n3=triggern-n1;
        auxiliar[3]=n3;
        if(triggern < n1)
          {
          Serial.println("trigger");
          }
      }
    while(now == last) 
      {      // wait until 1 ms elapses
      delayMicroseconds(Delay);
      now = millis();
      }
    writelink2();
  }

  writelink1();
}



void writelink1() 
{
  Serial.println("writelink1");
}

void writelink2() 
{
  
  Serial.println("writelink2");
  
  File dataFile = SD.open("curva1.txt", FILE_WRITE);
  
  if (dataFile) 
  {
    dataFile.println("testing 1, 2, 3.");
    dataFile.close();
    // print to the serial port too:
    
  }  
  // if the file isn't open, pop up an error:
  else 
  {
    Serial.println("error opening curva1.txt");
  } 
  
  while (true) {}
}

You may be running out of SRAM. Which Arduino model are you using? The short data type is a 16 bit value.

#define V 500
#include <SD.h>

// 1000 bytes eaten up here.
short curva[V];

Yep it seems I'm running out of RAM, I tested the code with a short with a size of 100 and it seems to work properly. I have an Arduino UNO R3.