Hello,
I am working on a project that requires me to retrieve saved data from an SD card and saving that data into a variable (most likely a character array or String). I need some guidance on how to achieve this.
Here is a sample of my code
Any help is appreciated
myFile = SD.open(profile, FILE_READ);
while(myFile.available()){
Serial.write(myFile.read()); // I dont want to write to monitor I want to save these characters
}
Not an array, but I think it's called a stream, and as such you can use any of the techniques from the Stream class which means Serial.
So for example in the sketch below, parseFloat() (clue's in the name ) parses some floats out of an SD card stream. (Not actually advocating the use of parsefloat() since it blocks afaik, but you get the idea.)
/*
BASED ON SD card file dump
This example shows how to read a file from the SD card using the
SD library and send it over the serial port.
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created 22 December 2010
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
float myVar1;
float myVar2;
float myVar3;
float myVar4;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("floats.txt"); //<<<<<<<<<<<<<<<<<<<<<< set file name here
// if the file is available, read from it:
if (dataFile)
{
while (dataFile.available())
{
//parse the next 4 values out of the file:
myVar1 = dataFile.parseFloat();
myVar2 = dataFile.parseFloat();
myVar3 = dataFile.parseFloat();
myVar4 = dataFile.parseFloat();
//print them to the monitor:
Serial.println(myVar1);
Serial.println(myVar2);
Serial.println(myVar3);
Serial.println(myVar4);
delay(1000);
//go back to the top for the next line...
}//while data available
//or ....
Serial.println(".... no more data");
dataFile.close();
}// if datafile
// if the file isn't open, pop up an error:
else {
Serial.println("error opening the data file");
}
}//setup
void loop()
{
}
As far as I know you can use any Serial technique to read an SD card; Robin2's serial tutorial might help.