Calling data from array

i want to call a data from waypoint array, but the sd card always get failed and stored 0 value

#include <SD.h>
File dataFile;
const int chipSelect = 4;
float waypoint [500];
int index = 0;

void setup() {
  Serial.begin(9600);
  Serial.print("Initializing SD Card....");
  if(!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not Present");
    return;
  }
  Serial.println("Card Initialized");
  File dataFile = SD.open("GPS_data.txt");
  if (dataFile) {
    for (index = 0; index <= 500; index++) {
      float input = dataFile.parseFloat();
      waypoint[index] = input;
      Serial.println(waypoint[index]);
      }
    dataFile.close();
   
  } else {
    Serial.println("File does not exist or named wrong");
  }
}

void loop() {
float x = waypoint[0];
Serial.print(x);
}

does the file exist on the SD card?
if so, can you read and print the file?
is it in the format you expect?

What Arduino are you using?

right, the file is from sd card. the output has already exactly as I wanted but I don't know how to store it in array

arduino uno r3

Please print out the first few lines of data on the file, so we can see the format.

You don't have the memory to run the SD card and your giant array.

The array alone takes more memory than is available,
and SD needs a 512 byte buffer for an open file.

1 Like

-6.97
107.63
-6.97
107.63
-6.97
107.63
-6.97
107.63
-6.97

that was the output from the code, but if I put the code inside the void loop, the program isn't running and got into an error

void loop() {
float x = waypoint[0];
Serial.print(x);
}

is it possible if i run this code on arduino mega 2560?

Yes, that should fit.

  • 2000 byte array
  • 128 byte Serial
  • 512 byte file buffer

Mega has 8k RAM.

1 Like

ok thx for the answer. it is work when I lower the number of array into 60 :grin:

BTW what kind of waypoint can be represented by six digits?

Do the math yourself, 60 will probably work.

The Uno has 2k total RAM.

If you can create a data format with a fixed length for each array object,
you can access the elements directly in the file via positioning.

You would not need an array at all.

can u give me some resources to learn how to access via positioning?

Instead of reading the file sequential, just position to the right spot, and read one element.

Files could also contain binary data, if human readability is not a concern.

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