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()
{
}