Isue with reading data of sd card

Hi everyone,

check my code below.
when reading number 4 on position 50 of the file groups i get -25536 while it should be 4
if itry it with less it works

can someone pls help me explain why?

Thanks to all of you!

my code:

#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  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("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  SD.remove("groups.txt");
  myFile = SD.open("groups.txt",FILE_WRITE);
 //myFile.seek(0);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to groups.txt...");
    myFile.print(",160158000000000000000000000000000000000000000000:40000!");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening groups.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("groups.txt");
  if (myFile) {
    Serial.println("groups.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  SD.remove("members.txt");
  myFile = SD.open("members.txt",FILE_WRITE);
 //myFile.seek(0);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to members.txt...");
    myFile.print("!");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening members.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("members.txt");
  if (myFile) {
    Serial.println("members.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  File myFile;
  myFile = SD.open("groups.txt", O_RDWR);
  myFile.seek( 50);
  Serial.print(myFile.position());
  long credit = 0;
  credit = (myFile.read() - '0' ) * 10000; Serial.println(credit);
  credit += (myFile.read() - '0') * 1000; Serial.println(credit);
  credit += (myFile.read() - '0') * 100; Serial.println(credit);
  credit += (myFile.read() - '0') * 10; Serial.println(credit);
  credit += myFile.read() - '0'; Serial.println(credit);
  myFile.close();
  myFile = SD.open("groups.txt");
  if (myFile) {
    Serial.println("groups.txt");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
      Serial.println(myFile.position());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening groups.txt");
  }
  Serial.println(credit);
}

void loop() {
  // nothing happens after setup
}

and here the serial for 3 and 4:

Initializing SD card...initialization done.
Initializing SD card...initialization done.
Writing to groups.txt...done.
groups.txt:
,160158000000000000000000000000000000000000000000:30000!Writing to members.txt...done.
members.txt:
!5030000
30000
30000
30000
30000
groups.txt
,1
12
63
04
15
56
87
08
09
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
:50
351
052
053
054
055
!56
30000
Initializing SD card...initialization done.
Initializing SD card...initialization done.
Writing to groups.txt...done.
groups.txt:
,160158000000000000000000000000000000000000000000:40000!Writing to members.txt...done.
members.txt:
!50-25536
-25536
-25536
-25536
-25536
groups.txt
,1
12
63
04
15
56
87
08
09
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
:50
451
052
053
054
055
!56
-25536

Pls explain this

long credit = 0;
 credit = (myFile.read() - '0' )

Can You explain what the intention is? Reading Your code finding out takes time....

Whatever data You show, the first one starts like ",1". That comma sign looks strange.

Receiving a negative number looks like out of sync or a mismatch regarding data typer being read.
Translate 65536-25536 = 40 000........ What value did You expect?

yes this is a part of a bigger program reading nfc tags and storing them witha specific format.
but back to my question if you check my code you will see that on position 50 the highest possible value should add up to 99 999 but above 39 999 things go wrong??
i should be receiving 40 000

the sd card saves data in ascii this: -'0' converts them back to regular numbers

Thx. Good to know.

If You use int the maximum positive value is 32767!
Try and use unsigned int, or word. Then max is 65535!

but I am using long?

tryed with unsigned int worked but cant figur out why it doesnt work with long as my max is 99 999 and unsigned int only works till 65535

Check how You get the data into the variable. Both int, unsigned int and word are 2 byte long. Both long and unsigned long are 4 bytes.
Know You need to start with long, or unsigned long. Any temporary int will overflow, wrap around, or so.

Sometimes compilers work can trick us. Try this:

  long credit = 0;
  credit = (myFile.read() - '0' ) * 10000L; Serial.println(credit);
  credit += (myFile.read() - '0') * 1000L; Serial.println(credit);
  credit += (myFile.read() - '0') * 100L; Serial.println(credit);
  credit += (myFile.read() - '0') * 10L; Serial.println(credit);
  credit += myFile.read() - '0'; Serial.println(credit);

I'm trying.....

Stepping up the tries...

  long credit = 0;
  credit = long((myFile.read() - '0' )) * 10000L; Serial.println(credit);
  credit += long((myFile.read() - '0')) * 1000L; Serial.println(credit);
  credit += long((myFile.read() - '0')) * 100L; Serial.println(credit);
  credit += long((myFile.read() - '0')8 * 10L; Serial.println(credit);
  credit += long(myFile.read() - '0'); Serial.println(credit);
1 Like

WORKS THANKS A MILLION!!
but still cant figure out why probly a compiler bug

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.