I am working on a system and need to read values on an sd card. The txt or csv will have multiple lines with 3 rows of values.
For Eg: 24.85,66.8, Buleji
24.82,67.02, Bath Island
24.87,67.03, Currie Reservoir
What I would like to achieve is to compare sensor reading with the first two values and if it matches ,return the corresponding character present in that row,otherwise search for next line.
This text file has 5 lakh lines.
I am very new to programming..
Please help! Thanks
this can hel you
/*
SD card read line
Split value ","
compare value A , value B
Print value C
By Carmelo G.
*/
#include <SPI.h>
#include <SD.h>
//*************************************************************************
//** PROGRAMM VARIABLE AND CONST **
//*************************************************************************
const int chipSelect = 53; // ++ SD pin selector
const int MAX_STR_LEN = 64; // the largest string you will process
char stringBuffer[MAX_STR_LEN]; // static buffer for computation
float valLat = 4.87; //
float valLon = 67.03; //
char str_float[5]; // Static buffer for float to char conversion
char *dtostrf(double val, signed char width, unsigned char prec, char *s);
File myFile; //
//*************************************************************************
//** SETUP **
//*************************************************************************
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin( chipSelect )) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
delay( 1000 );
// READ VALUE
// valLat = ??;
// valLon = ??;
// COMPARE VALUE
// open the file for reading:
myFile = SD.open("values.txt");
if (myFile) {
Serial.println("values.txt:");
// read from the file until there's nothing else in it:
char lineRead[ MAX_STR_LEN ];
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array for store the char
while ( myFile.available() ) {
inChar = myFile.read(); // Read a character
//-------------------------------------------------------------------
lineRead[ index ] = inChar; // Store it
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
//-------------------------------------------------------------------
if ( inChar == '\n' ) {
lineRead[index] = '\0'; // Null terminate the string
Serial.println( lineRead );
split_message( lineRead );
for (int i = 0 ; i < MAX_STR_LEN ; i++ ) {
lineRead[i] = 0;
}// End For
index = -1;
//-----------------------------------------------------------------
}
else
{
//-----------------------------------------------------------------
index++; // Increment where to write next
//-----------------------------------------------------------------
}
}
// End File
Serial.println( lineRead );
split_message( lineRead );
for (int i = 0 ; i < MAX_STR_LEN ; i++ ) {
lineRead[i] = 0;
}// End For
index = -1;
// close the file:
myFile.close();
}
}
//**************************************************************************
//** SPLIT MESSAGE RX FUNCTION **
//**************************************************************************
void split_message( char* string )
{
Serial.print("Split:");
Serial.println( string );
char *str;
char *p;
strncpy(stringBuffer, string, MAX_STR_LEN); // copy source string
// ***********************************************************************
// **split using comma loop while str is not null get subsequent tokens **
// ***********************************************************************
int pos = -1;
for ( str = strtok_r(stringBuffer, ",", &p); str; str = strtok_r(NULL, ",", &p))
{
Serial.print( F("[") ); Serial.print( str ); Serial.println( F("]") );
// check final status
if ( pos == 1 ) {
Serial.print( F("OK: [") ); Serial.print( str ); Serial.println( F("]") );
}
// Compare 2° value
if ( pos == 0 ) {
Serial.print( F("str: [") ); Serial.print( str ); Serial.println( F("]") );
dtostrf( valLon, 4, 2, str_float );
Serial.print( F(" 2 ?: [") ); Serial.print( str ); Serial.print( F("==") ); Serial.print( str_float ); Serial.println( F("]") );
if ( Comp( str, str_float ) == 0 ) {
pos = 1;
}
}
// Compare 1° value
if ( pos == -1 ) {
Serial.print( F("str: [") ); Serial.print( str ); Serial.println( F("]") );
dtostrf( valLat, 4, 2, str_float );
Serial.print( F(" 1 ?: [") ); Serial.print( str ); Serial.print( F("==") ); Serial.print( str_float ); Serial.println( F("]") );
if ( Comp( str, str_float ) == 0 ) {
pos = 0;
}
}
} // END for
}
//*************************************************************************
//** Compare Value **
//*************************************************************************
char Comp( char* inData, char* This )
{
if ( strcmp( inData, This ) == 0 ) {
return (0);
} else {
return (1);
}
}
/*
SD card read line
Split value ","
compare value A , value B
Print value C
By Carmelo G.
*/
#include <SPI.h>
#include <SD.h>
//*************************************************************************
//** PROGRAMM VARIABLE AND CONST **
//*************************************************************************
const int chipSelect = 53; // ++ SD pin selector
const int MAX_STR_LEN = 64; // the largest string you will process
char stringBuffer[MAX_STR_LEN]; // static buffer for computation
String gpsLocation; //
boolean finded = false; //
float valLat = 24.87; //
float valLon = 67.03; //
char str_float[5]; // Static buffer for float to char conversion
char *dtostrf(double val, signed char width, unsigned char prec, char *s);
File myFile; //
//*************************************************************************
//** SETUP **
//*************************************************************************
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin( chipSelect )) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
delay( 1000 );
// READ VALUE
// valLat = ??;
// valLon = ??;
// COMPARE VALUE
// open the file for reading:
myFile = SD.open("values.txt");
if (myFile) {
Serial.println( F(" Read values.txt:") );
// read from the file until there's nothing else in it:
char lineRead[ MAX_STR_LEN ];
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array for store the char
Serial.println( F("Start search:") );
while ( myFile.available() ) {
inChar = myFile.read(); // Read a character
lineRead[ index ] = inChar; // Store it
//-------------------------------------------------------------------
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
//-------------------------------------------------------------------
if ( inChar == '\n' || index == MAX_STR_LEN - 1 ) {
lineRead[index] = '\0'; // Null terminate the string
split_message( lineRead );
// Clean charArray
for (int i = 0 ; i < MAX_STR_LEN ; i++ ) {
lineRead[i] = 0;
}// End For
index = 0;
if ( finded == true ) {
Serial.print( F("OK: [") ); Serial.print( gpsLocation ); Serial.println( F("]") );
break;
myFile.close(); // close the file:
}
}
else
{
index++; // Increment where to write next
}
}
if ( finded == false ) {
Serial.print( F("[NOT FINDED]") );
}
finded = false;
// close the file:
myFile.close();
}
}
//**************************************************************************
//** SPLIT MESSAGE RX FUNCTION **
//**************************************************************************
void split_message( char* string )
{
Serial.println( F("-------------------------") );
Serial.print( F("Split:") );
Serial.println( string );
char *str;
char *p;
strncpy(stringBuffer, string, MAX_STR_LEN); // copy source string
//***********************************************************************
//**split using comma loop while str is not null get subsequent tokens **
//***********************************************************************
int pos = -1;
for ( str = strtok_r(stringBuffer, ",", &p); str; str = strtok_r(NULL, ",", &p))
{
// check final status
if ( pos == 1 ) {
finded = true;
gpsLocation = str;
}
// Compare 2° value
if ( pos == 0 ) {
dtostrf( valLon, 4, 2, str_float );
Serial.print( F(" Lon ?: [") ); Serial.print( str ); Serial.print( F("==") ); Serial.print( str_float ); Serial.println( F("]") );
if ( Comp( str, str_float ) == 0 ) {
pos = 1;
} else {
break;
}
} // END if
// Compare 1° value
if ( pos == -1 ) {
dtostrf( valLat, 4, 2, str_float );
Serial.print( F(" Lat ?: [") ); Serial.print( str ); Serial.print( F("==") ); Serial.print( str_float ); Serial.println( F("]") );
if ( Comp( str, str_float ) == 0 ) {
pos = 0;
} else {
break;
}
} // END if
} // END for
}
//*************************************************************************
//** Compare Value **
//*************************************************************************
char Comp( char* inData, char* This )
{
if ( strcmp( inData, This ) == 0 ) {
return (0);
} else {
return (1);
}
}