-1

Please help with the code. Reading a file from flash memory. The file contains only 4 numbers in two lines: 254 128 and 0 127. The program must also read these 4 numbers: 254 128 0 127 This is what actually happens:

LittleFS Matrix Read Test Volume size: 125 MByte File opened successfully. Reading and sending data... 5053 5232 4950 5613 1048 3249 5055 1310 Data has been sent successfully.
Thanks in advance for the community's support.

while (file.available()) {
  for (int i = 0; i < rows; i++) { 
    for (int j = 0; j < cols; j++) { 
      int byteValue = file.read();  // Reading one byte 
      Serial.print(byteValue);
          
      }
   //   Serial.print(" ");  // Separator between bytes for clarity
    }
    

  break;  // End the loop once the data has been sent once
 }

 file.close();
 Serial.println("\nData has been sent successfully.");
}

1 Answer 1

0

Your file does not contain 4 numbers but the ascii representation of those 4 numbers. You can check by looking at an ascii table (e.g. https://www.torsten-horn.de/techdocs/ascii.htm) and translate your printout to the actual numbers

50 -> '2'
53 -> '5'
52 -> '4'
32 -> 'space'  
etc. 

either change your file so that it really contain the actual bytes or convert the ascii representation of the numbers in the file to bytes in your code.

It looks like you are using an Arduino compatible board. So, you can use something like

//....
for (int i = 0; i < 4; i++)
{
   uint8_t b = dataFile.parseInt();
   Serial.println(b);
}
//...

to read out 4 bytes from your file.

2
  • I can't convert the data in the file. How can I convert ascii table to the actual numbers when reading the file?
    – Igor
    Commented Nov 15 at 17:34
  • Added an Arduino example how to extract the numbers from your text file.. You might be better off asking this kind of questions in the Arduino forum.
    – luni64
    Commented Nov 15 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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