0

I am new to arduino I am trying to create an arduino project that Saving a .txt file to SD Card and Reading the txt file then save it to a variable data type "Long" named 'savednum'. I had successfully can save the txt file and read it, however the problem lies in storing the read txtfile, when I read the txtfile and stored it in savednum and then serial print the savednum it gives different/ random numbers which is not equal to the data in the .txtfile that I store and read. I am wondering what seem to be wrong? Any suggestion is really appreciated. I thank you in advance.

#include <TMRpcm.h>
 #include <SD.h>
 #include <SPI.h>
 #define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328

 TMRpcm tmrpcm; // create an object for use in this sketch

File myFile;

char serialData;


int i = 0;
long n1 = 123456789;
long n2 = 245678918;

long savednum =0;


void setup()
{
Serial.begin(9600);
while (!Serial) {

 }



Serial.print("Initializing SD card...");

 if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("initialization failed!");
 while (1);
}
Serial.println("initialization done.");


 myFile = SD.open("NEWDATA.txt", FILE_WRITE);


if (myFile) {
Serial.print("Writing to NEWDATA.txt...");
myFile.println(n1);
myFile.println(n2);

myFile.close();
Serial.println("done.");
} else {

Serial.println("error opening NEWDATA.txt");
}    

myFile = SD.open("NEWDATA.txt");
if (myFile) {
Serial.println("NEWDATA.txt:");

while (myFile.available()) {
  Serial.write(myFile.read());  
// here the code I tried to read .txt file and store it in long   
savednum = myFile.read();
Serial.println(savednum);
}

myFile.close();

}


}




void loop()
 {

  } 
  • Side note: Do yourself and your readers a favor and format the code properly, either by using Arduino IDE features (tutorial here) or some online beautifier. It'll be much easier to spot errors this way. – Maximilian Gerhardt May 9 '18 at 9:59
0

myFile.read() reads just one byte from the file - not the whole file or even a whole line.

You're reading one byte, then printing it. Then reading a second byte, storing that in a variable, then printing the variable. Then you loop back to the beginning again.

So you're printing alternate "odd" bytes, and storing / overwriting the variable with "even" bytes. So you just get nonsense.

You're storing the data in your file as ASCII text, so it's necessary to read the entirety of each line and then convert that string of characters into a number. Or read each character in turn and manipulate your total value for the line.

For example:

long savedNum = 0;
long numberOne = 0;

while (myFile.available()) { // Something left to read
    char c = myFile.read(); // Get the next character
    if (isdigit(c)) { // It's a number 0-9
        savedNum *= 10; // Make room by decimal shifting to the left
        savedNum += (c - '0'); // Convert the character to a number and add it to the total
    } else if (c == '\n') { // End of line
        Serial.print(F("The number is: "));
        Serial.println(savedNum); // Show us the number
        numberOne = savedNum;  // Store it somewhere else - enhance this for multiple numbers.
        savedNum = 0;  // Reset to 0 ready to read the next line
    }
}

The Arduino API (Stream class) also includes some handy helper functions, such as:

int num = myFile.parseInt(); // get the next integer

There's no parseLong() though, but despite its name, parseInt() actually returns a long. That reads characters up until it gets a non-numeric character and converts them to a long using the same method as in my example. It doesn't distinguish lines, though, so can be used to get separate numbers on the same line, such as 32847,2345534,12,-43.

With your file though you may need to filter out the extra \r that may be in there from the line endings or you'll get missing numbers.

  • Hi thank you very much for your answer it really helped me alot now I am successfully store the data from the .txt file to a variable however I have one last question because here I saved 18characters in the txt file I wanted it to split by 9 characters then distribute it in to two data type long variable named Storenum1 and storenum2 is it possible? Hope you can help me. I am really thankful for your help. – Megumichan May 9 '18 at 9:38
  • You're not storing 18 characters. You're storing 2 strings of up to 9 characters each, plus line endings (because you wrote with println). So you get two lines in your file of nnnnnnnnn\r\n. My example will read each line in turn - it's up to you to decide what to do with the savedNum value for each line. – Majenko May 9 '18 at 9:44
  • Thank you now I understand thank very much for you good explanation and answer 🙂 I tried to study how to store in two variables. I really appreciate your help thank you – Megumichan May 9 '18 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.