Trouble with Arduino SD library.

Status
Not open for further replies.
So this chunk of code is supposed to take incoming characters from the USB port and save them 400 at a time to a text file on a micro SD card. (Micro sd adapter+teensy 3.0). So, I don't know what I'm doing wrong, but it doesn't seem to work. My cohort tested the our hardware setup with one of the built in examples for the SD library, so we know it's not the hardware. Any idea about what I might be doing wrong? (the ss conector is on pin 10, which is Serial 2 on the teensy 3.0).

Code:
#include <SD.h>

File myFile;
int count = 0; 
const int chipSelect = 10;
void setup() 
{

}

void loop() 
{ 
  Serial2.begin(9600); //This is the SD connection
  Serial.begin(9600); //This is the usb connection
  pinMode(10, OUTPUT);
  SD.begin(10);
  char name[] = "RTLinfo.txt";
  char text[400];
  int len_text = 0; 
  while (Serial.available()!=0 || len_text!=0) //This loop will only end when there are no characters left in either the array or the buffer.
  {

    if(len_text < 400 && usb_serial_available!=0)
      
    {
      //This section just adds characters from the buffer to the array until the array has 400 characters, or until there are no more in the buffer.
      char incomingByte = Serial.read(); 
      text[len_text] = incomingByte; 
      len_text ++;
    }
    else
    {
      //This section uses functions fron the SD library to create/open the file named "RTLinfo" and then save the string of 400 characters into the sd card. 
      myFile = SD.open(name, FILE_WRITE);
      for (int j = 0; j < len_text; j++) 
      {     
        myFile.println(text[j]);
      }
      //This section closes the file and resets the array "text" and the count. From here the loop should repeat with the next 400 characters.
      myFile.close();
      char text[400];
      len_text=0;
    } 
  }
}
 
Last edited:
but it doesn't seem to work.
What does it do correctly, and not?

What's the reason for using Serial2? That's a UART port, unrelated to the SD card.

Please edit the post - go to advanced editing mode, click the # to create a block for source code. Then copy/past your source code in this block, preserving the indentations. Hard to read as a block of text as originally posted.

Thanks.
 
Well, the code compiles and runs without error but whenever I check the SD card, there is nothing saved on it. The SD adapter is connected on pin 10, and I was of the understanding that pin 10 is serial 2. Also, I set up my breadboard like the one in this picture.

MicroSD connections to Teensy3.jpg
 
Last edited:
It is probably unrelated, but wonder about the line:
Code:
      char text[400];
Right after the myfile.close() ?

Since it is defined after the other usages, it's scope probably does not interfere? But I would sure remove it and see if it helps.

Edit: Also, on another thread I have issues reading SD card on some of the Adafruit display cards. The call to SD.begin() is failing. You might want to check the return value here and see if this is succeeding for you.
 
Last edited:
Status
Not open for further replies.
Back
Top