I am trying to read a row of record from SD card and display on LCD. I am using ( the bad ) String. It works well and i am not in any tight corner for Flash space as the whole code is just about 36K on a Mega 2560. But then was wanting to find out a more elegant and optimum way to do this as what i have done seems pretty convoluted ! :
The first part of code snippet : Declare String variables and parse the data from SD card :
// Declare Globals
String SlNo;
String Priority;
String Freq;
String Route;
String RouteNo;
String RFID;
String EquipID;
String EquDesc;
String GrType;
String GrQty;
...
...
...
// FUNCTION TO RETRIEVE SPECIFIC RECORD AND LOAD FEILDS INTO GLOBAL VARIABLES
void parseCharString (int recPosition ) {
char letter;
String readString;
int deLim1, deLim2, deLim3, deLim4, deLim5, deLim6, deLim7, deLim8, deLim9, deLim10, deLim11;
File dataFile = SD.open("ACTUAL.CSV", FILE_READ); // Open the file
dataFile.seek(recPosition); // Go to the required row
if (dataFile) {
while ( dataFile.available()) {
letter = dataFile.read();
if (letter == '>') {
dataFile.close();
deLim1 = readString.indexOf('<');
deLim2 = readString.indexOf(',');
SlNo = readString.substring( deLim1 + 1, deLim2);
deLim3 = readString.indexOf(',', deLim2 + 1);
Priority = readString.substring(deLim2 + 1, deLim3);
deLim4 = readString.indexOf(',', deLim3 + 1);
Freq = readString.substring(deLim3 + 1, deLim4);
deLim5 = readString.indexOf(',', deLim4 + 1);
Route = readString.substring(deLim4 + 1, deLim5);
deLim6 = readString.indexOf(',', deLim5 + 1);
RouteNo = readString.substring(deLim5 + 1, deLim6);
deLim7 = readString.indexOf(',', deLim6 + 1);
RFID = readString.substring(deLim6 + 1, deLim7);
deLim8 = readString.indexOf(',', deLim7 + 1);
EquipID = readString.substring(deLim7 + 1, deLim8);
deLim9 = readString.indexOf(',', deLim8 + 1);
EquDesc = readString.substring(deLim8 + 1, deLim9);
deLim10 = readString.indexOf(',', deLim9 + 1);
GrType = readString.substring(deLim9 + 1, deLim10);
deLim11 = readString.indexOf('>', deLim10 + 1);
GrQty = readString.substring(deLim10 + 1, deLim11);
readString = "";
break;
}
else {
readString += letter;
}
}
}
else {
lcd.clear();
lcd.print(F("ACTUAL.CSV"));
lcd.setCursor(0, 1);
lcd.print(F(" SD File Error2 ! "));
lcd.setCursor(0, 2);
lcd.print(F(" Reset and Verify.."));
while (1); // Hold the message...and wait for reset.
}
}
...
...
...
Now for code to display the parsed data on to a LCD
// FUNCTION TO DISPLAY THE DETAILS AND AWAIT USER RESPONSE...
void awaitScanDisp() {
lcd.clear();
char buf[21];
int len = sizeof(buf); // Directly displaying String variable has issues..
Route.toCharArray( buf, len);
lcd.setCursor(0, 0); // Part of first line
lcd.print(buf);
EquipID.toCharArray( buf, len);
lcd.setCursor(12, 0); // Remaining part of first line
lcd.print(buf);
EquDesc.toCharArray( buf, len);
lcd.setCursor(0, 1); // Second line
lcd.print(buf);
GrType.toCharArray( buf, len);
lcd.setCursor(0, 2); // Third line
lcd.print(buf);
lcd.setCursor(0, 3); // Fourth line. ( Third line left blank )
lcd.print(F( "SCAN RFID! ToGo:"));
getNoOfRecords();
lcd.setCursor(18, 3);
lcd.print( numToGrease);
}