Spicy O'merta
Member
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:
