Hello,
I am currently working on a project which involves reading data stored on an SD card. I'm trying to use some of the program from example 5 that is listed on the Serial Input Basics tutorial here: Serial Input Basics - updated - Introductory Tutorials - Arduino Forum.
I'm trying to make a program which parses through the data stored on the card and will save the user ID and Passwords to an array.
In order to do this however I first need to feed all of the data stored on the SD card and place it through the function which parses data. But I'm stuck on this step.
I'm assuming that you have to put some sort of data into the array called "receivedChars", but I'm kind of stuck on how to go about doing it from data stored on a .txt file on an SD card.
I've tried creating a string and saving the data from the .txt file into the string, but that didn't work. So
So far, the code that I have is:
#include <SD.h>
#include <SPI.h>
// declare files to read/write from
File userData;
const int chipSelect = 7;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
void setup() {
Serial.begin(9600);
pinMode(chipSelect, OUTPUT);
// Initialize the SD Card
if (SD.begin()) {
Serial.println("SD card is ready to use.");
}
else {
Serial.println("SD card initialization failed");
return;
}
userData = SD.open("userinfo.txt");
if (userData) {
Serial.println(" ");
// Reading the whole file
while (userData.available()) {
Serial.write(userData.read());
}
userData.close();
}
else {
Serial.print("error opening ");
Serial.println(userData);
}
}
void loop() {
int stringLen = userDataString.length();
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
}
My file stored on the SD card is called userinfo.txt and looks like this:
user<Username>
userid<1,2,3,4>
password<4,5,6,7>
Thank you!