I'm not new to Arduino coding, but it's the first time i'm working with SD card, files. I'm always tripped up by string handling and this is probably my issue, but it may be file reading also. For info, I'm using a Teensy 3.2 with the Audio card reader. I seem to be able to read the data on the file, but it's not storing it correctly.
#include <SPI.h>
#include <SD.h>
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// SD - Soundcard
char fileDescription[14];
char line[14];
char* descriptionLine[10];
int descriptionCount = 0;
void setup() {
// --------------------------------- SD Card Setup
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// Display "no SD Card" but allow processing to continue
Serial.println("No SD CARD!");
}
if (SD.exists("INDEX.TXT")) {
Serial.println("Reading SD");
File fileDescription = SD.open("INDEX.TXT");
if (fileDescription) {
// Read from the file until there's nothing else in it
while (fileDescription.available()) {
// clear the linebuffer
memset(line, '\0', sizeof(line));
// read a line fromfile
fileDescription.readBytesUntil('\n', line, sizeof(line) - 1);
// Print the line as read
// Serial.print(line);
Serial.print(descriptionCount);
Serial.print(" - ");
Serial.println(line);
// Put filename in Description array
descriptionLine[descriptionCount] = line;
// also tried: strcpy(descriptionLine[descriptionCount],line);
descriptionCount++;
} // More to read
} // Done reading
// close the file:
fileDescription.close();
// Verify the results
Serial.println();
Serial.println("Description array");
for (int i = 0; i < descriptionCount; i++) {
Serial.print(i);
Serial.print(" = ");
Serial.println(descriptionLine[i]);
}
} else { // if the file didn't open, print an error:
Serial.println("error");
} // Error, but continue processing
} // End Setup
void loop() {
// put your main code here, to run repeatedly:
} // End Loop
Well done using autoformat and code tags when posting the code!
Can expand the description of the file handling issue? You manage to read files from the SD. Where do Yo intend to store? What goes wrong, how....?
The problem is twofold. You are setting every element of descriptionLine to point to line. When you're done reading, line contains num9 and so your print loop prints it ten times.
What you need is strcpy, but that won't work either because descriptionLine is an array of pointers that have not got any memory to point to, so strcpy will likely crash your program.
Try making descriptionLine a 2d array of char so you have some space available to copy the lines from the file into.
Yeah, i think you're right that it's a character array problem i have, but i still have a hard time wrapping my head around it. You mean a 2D array like:
char* descriptionLine[14][10];
but then how do i denote the first part of the array to put it into the char array?
You set up 10 pointers, but not to any specific memory block, and you are getting into issues of dynamic memory allocation. I'm certain there are appropriate and safe ways to handle this on the Teensy, but I am only familiar with the practices of using allocated memory as you do with the smaller processors like the AT328.
To fill the descriptionLine arrays you can use strcpy as the line array is null terminated and will always be smaller or the same size as descriptionLine.
strcpy(descriptionLine[descriptionCount],line);//you only need the first index
#include <SPI.h>
#include <SD.h>
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// SD - Soundcard
char fileDescription[14];
char line[14];
//char* descriptionLine[10];
char descriptionLine[10][14];//descriptionLine[10][sizeof(line)];
int descriptionCount = 0;
void setup() {
// --------------------------------- SD Card Setup
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// Display "no SD Card" but allow processing to continue
Serial.println("No SD CARD!");
}
if (SD.exists("INDEX.TXT")) {
Serial.println("Reading SD");
File fileDescription = SD.open("INDEX.TXT");
if (fileDescription) {
// Read from the file until there's nothing else in it
while (fileDescription.available()) {
// clear the linebuffer
memset(line, '\0', sizeof(line));
// read a line fromfile
fileDescription.readBytesUntil('\n', line, sizeof(line) - 1);
// Print the line as read
// Serial.print(line);
Serial.print(descriptionCount);
Serial.print(" - ");
Serial.println(line);
// Put filename in Description array
//descriptionLine[descriptionCount] = line;
strcpy(descriptionLine[descriptionCount],line);
// also tried: strcpy(descriptionLine[descriptionCount],line);
descriptionCount++;
} // More to read
} // Done reading
// close the file:
fileDescription.close();
// Verify the results
Serial.println();
Serial.println("Description array");
for (int i = 0; i < descriptionCount; i++) {
Serial.print(i);
Serial.print(" = ");
Serial.println(descriptionLine[i]);
}
} else { // if the file didn't open, print an error:
Serial.println("error");
} // Error, but continue processing
} // End Setup
void loop() {
// put your main code here, to run repeatedly:
} // End Loop
I guess because the 'char' type is a form of array itself? I fear i'll never truly get character and string handling, but i really do appreciate you guiding me thru it and providing a workable answer!