Reading and comparing strings from SD files

Hey everyone!

I have an idea but not sure how to go about it.

I want to use the arduino (mega) to read a string (single line) and then compare that string to another string (single line) from a different file. The files are being read from an SD card.

I want the arduino to do this until a match is found.

If further explanation is needed let me know! Also do any of you know how the cursor works in text files for the arduino?

Also if I did anything wrong in this post or whatever, I'm sorry! This is my first post!

What have you tried ?

Have you tried the examples that come with the SD library ?
Can you read a character from a file and print it then read the next character and print it until the end of file is reached ?
Can you read a line of text from a text file, put it into an array of chars and print it ?
Does the program need to compare say line 1 of file 1 with line 1 of file 2 or perhaps line 1 of file 1 with every line of file 2 ?

do any of you know how the cursor works in text files for the arduino?

When you read a character from a file the current position in the file is incremented ready for the next read operation. Is that what you are asking ?

I learned the basics of the SD file library from one example and reading the forums.

Currently I am simply using "MyFile.ReadStringUntil(48);", for example.

Then I use a while loop to compare the two strings.

I ask about the cursor because it isnt very clear on its position.
I have this in one of my text files

standby:000
sp_clk_up:017
sp_clk_dn:034

I need to skip the following 3 digits and then a '\n' or '\r' for the next line, if the strings dont match.

At the risk of replying with too much information I currently use a for loop and an array of 3 to read the
digits following the colon. Could that be sufficient?

At the risk of replying with too much information

Providing too much information would be rare and you certainly haven't done it.

Please post your current code, using code tags when you do. If you do not know what that means then please read this before posting a programming question

At the risk of replying with too much information I currently use a for loop and an array of 3 to read the
digits following the colon. Could that be sufficient?

Read this before posting a programming question ...

  1. Getting help on the forum

There are quite a few experienced people on the forum anxious to help you, and help you get as much as you can out of your Arduino. You can help them do that by making helpful posts:

Make an informative subject description, not "help me, I'm a noob", nor something in all capitals. Try to avoid saying "urgent". That's your problem, not ours.
Describe your problem in detail.
If it relates to an electronics part (chip or board), give the exact part number and preferably a link to the data sheet.
Describe how you have connected things like switches. Are they wired to ground? Or +5V? Are there pull-up or pull-down resistors? Post a circuit if there is doubt.
Post your complete sketch (program code)! If you don't you waste time while people ask you to do that.
When you post your code put it between

...

tags. You can do that by hitting the </> button above the posting area.
If you get an error, post the error (copy and paste). Not just "I got an error".
With coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code.
If you have debugging information in your sketch, post your debugging displays.
Describe what you expected to happen, and what actually happened. Not just "it doesn't work".
If possible, describe what you are really trying to do, not what you think might work. For example "I am trying to turn on an aquarium heater and pump at the same time", not "how do I break out of an interrupt?".
Don't double-post (cross-post). Your question will be noticed. If you post it in multiple places you will just annoy people who might otherwise have answered.

Here is my current code. I still have a lot of Serial prints for debugging this.

void script(){

  
            Script = SD.open("script.txt");
            Serial.println("Opened textfile: 'script.txt'");
            Serial.println("Script cursor position: ");
            Serial.println(Script.position(), DEC);
  
            Script.seek(index_cmd);
            
//            Serial.print("index_cmd = ");
//            Serial.println(index_cmd);
//            Serial.println("Script cursor position: ");
//            Serial.println(Script.position(), DEC);
            
            ScriptCommandLine = Script.readStringUntil(48);
            Serial.print("ScriptCommandLine: ");
            Serial.print(ScriptCommandLine);
            Serial.println("\n");
            
            index_cmd += Script.position();
            Script.close();
            Serial.println("Closed textfile: 'script.txt'");
  
            Commands = SD.open("commands.txt");
            Serial.println("Opened textfile: 'commands.txt'");
            Serial.print('\n');
            

      while(!(ScriptCommandLineTest == ScriptCommandLine)){

            Serial.println(Commands.position(), DEC);
            ScriptCommandLineTest = "                ";
            ScriptCommandLineTest = Commands.readStringUntil(58);
            //Serial.println(ScriptCommandLine);
            Serial.println(Commands.position(), DEC);
            Serial.println(ScriptCommandLineTest);
            
      for(int i = 0; i < 3; i++){

            index_byte[i] = Commands.read() - 48; 
                
            }
      }
            index_int = index_byte[0] * 100 + index_byte[1] * 10 + index_byte[2];  
            Serial.println(index_int);
            
            Commands.close();
            Serial.println("Closed textfile: 'commands.txt'");

            //NotFirstCommandLine = true;
}

Here is my current code.

Did you miss the part of the advice that says

Post your complete sketch (program code)! I

#include <SPI.h>
#include <SD.h>
#include <String.h>

#define   DATA_OUT    23
#define   DATA_CLK    25 
#define   LATCH_CLK   27
#define   INSTR_OE    29
#define   CLK         31


byte code_byte[16];
byte micro_code[2] = {0,0};

byte index_byte[3] = {0,0,0};
byte index_cmd     = 0;
int  index_int     = 0;

String ScriptCommandLine     = "                ";
String ScriptCommandLineTest = "                ";

//bool NotFirstCommandLine = false;

File Code;
File Script;
File Commands;


//fetch command characters. define location of mircocode.
void script(){

  
            Script = SD.open("script.txt");
            Serial.println("Opened textfile: 'script.txt'");
            Serial.println("Script cursor position: ");
            Serial.println(Script.position(), DEC);
  
            Script.seek(index_cmd);
            
//            Serial.print("index_cmd = ");
//            Serial.println(index_cmd);
//            Serial.println("Script cursor position: ");
//            Serial.println(Script.position(), DEC);
            
            ScriptCommandLine = Script.readStringUntil(48);
            Serial.print("ScriptCommandLine: ");
            Serial.print(ScriptCommandLine);
            Serial.println("\n");
            
            index_cmd += Script.position();
            Script.close();
            Serial.println("Closed textfile: 'script.txt'");
  
            Commands = SD.open("commands.txt");
            Serial.println("Opened textfile: 'commands.txt'");
            Serial.print('\n');
            

      while(!(ScriptCommandLineTest == ScriptCommandLine)){

            Serial.println(Commands.position(), DEC);
            ScriptCommandLineTest = "                ";
            ScriptCommandLineTest = Commands.readStringUntil(58);
            //Serial.println(ScriptCommandLine);
            Serial.println(Commands.position(), DEC);
            Serial.println(ScriptCommandLineTest);
            
      for(int i = 0; i < 3; i++){

            index_byte[i] = Commands.read() - 48; 
                
            }
      }
            index_int = index_byte[0] * 100 + index_byte[1] * 10 + index_byte[2];  
            Serial.println(index_int);
            
            Commands.close();
            Serial.println("Closed textfile: 'commands.txt'");

            //NotFirstCommandLine = true;
}

//fetch microcode. convert the microcode to usable form.
void microcode(){

  
            Code = SD.open("code.txt");
            Serial.println("Opened textfile: 'code.txt'");
            Code.seek(index_int);


      for (int i = 0; i < 16; i++){
        
            code_byte[i] = Code.read() - 48;
            
            
       }//Serial.print("code_byte: "); 

          
      for (int i = 0; i < 16; i++){
        
            //Serial.print(code_byte[i], DEC);
            
            
      }//Serial.println("\n");


            Code.close();
            Serial.println("Closed textfile: 'code.txt'");

            
      for(int i = 0; i < 8; i++){

            micro_code[0] += pow(2, 7 - i) * code_byte[i] + 0.0001;
            
//            Serial.print("i = ");
//            Serial.println(i, DEC);
//            Serial.print("micro_code[0] = ");
//            Serial.println(micro_code[0], DEC);
            
    
      }//Serial.println("\n");

 
      for(int i = 8; i < 16; i++){
        
            micro_code[1] += pow(2, 15 - i) * code_byte[i] + 0.0001;
            
//            Serial.print("i = ");
//            Serial.println(i, DEC);
//            Serial.print("micro_code[1] = ");
//            Serial.println(micro_code[1], DEC);

            
      }
  
}

//send data to shift registers
void mcc_out(){ 

  
            shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, micro_code[1]);
            shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, micro_code[0]);

            digitalWrite(LATCH_CLK, HIGH);
            delay(10);
            digitalWrite(LATCH_CLK, LOW);

            digitalWrite(INSTR_OE, LOW);
            
}

void setup() {

            pinMode(INSTR_OE, OUTPUT);
            pinMode(CLK, OUTPUT);
  
            digitalWrite(INSTR_OE, HIGH);
            digitalWrite(CLK, LOW);

            pinMode(DATA_OUT, OUTPUT);
            pinMode(DATA_CLK, OUTPUT);
            pinMode(LATCH_CLK, OUTPUT);
    
            digitalWrite(DATA_OUT, LOW);
            digitalWrite(DATA_CLK, LOW);
            digitalWrite(LATCH_CLK, LOW);

            shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, B01111111);
            shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, B01111111);

            digitalWrite(LATCH_CLK, HIGH);
            delay(10);
            digitalWrite(LATCH_CLK, LOW);

            digitalWrite(INSTR_OE, LOW);  


            delay(1000);
            Serial.begin(9600);

            
      while (!Serial) {; // wait for serial port to connect. Needed for native USB port only
      }Serial.print("Initializing sd card...");


      if (!SD.begin(53)) {
        
            Serial.println("failed!");          
            while(1);

            
      }Serial.println("initialization done.");


           script();
         //Script: Retrieve a line of command from "script.txt". 
         
           microcode();
         //Code: Determine which command from "script.txt" by searching "commands.txt".
         //      Retrieve location of microcode from "commands.txt" and find microcode from "code.txt". 
           
           mcc_out();
         //Output microcode from the command found "script.txt".

           Serial.println(micro_code[0], BIN);
           Serial.println(micro_code[1], BIN);
           
           delay(5000);
           
           script();
           microcode();
           mcc_out();

           Serial.println(micro_code[0], BIN);
           Serial.println(micro_code[1], BIN);
           
            
}

void loop() {
  // put your main code here, to run repeatedly:

}