简体   繁体   中英

Data in SD card convert it to array arduino?

My data is in a text file in an SD card, and I am trying to make it an array in Arduino. My data is just a bunch of integers that will look like 270 numbers each one on a line.

122
50
255
0 
96

I have 270 numbers like this. I just want Arduino to create an array of size 270 so I can use that data. Later on, I am going to pull out an element to put it somewhere, and so on. Following the example given I can read the data from the SD card. I'm having a hard time creating it into an array.

This is what I have tried so far. In my code the array I want String3_5 just gives me numbers 0 to 269 incrementing.

#include <SD.h>
const int chipSelect = 4;
int String3_5 [270];
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("Strip3_5.txt");
  if(dataFile) {
    while(dataFile.available()) {
      Serial.write(dataFile.read());
      String3_5[index] = Serial.read();
      index++;
      String3_5[index] = '\0';
    }
    dataFile.close();
  } else {
    Serial.println("File does not exist or named wrong");
  }
}

void loop() {
}

How are your integers saved into your file? ASCII? Or binary. What your code does for the moment is read 8-bit values, send them to the UART/Terminal, then read the terminal and save a byte into the array. I don't think that's what you want...

SD.read()

Read a byte from the file.
read() inherits from the Stream utility class.

Also the line String3_5[index] = '\\0'; looks more like something used for strings...

Assuming that your file is actually an ASCII file, what you need to do is implement code that reads ASCII numbers, converts them to integers, and then save that.

These codes should be what u are looking for

 File dataFile = SD.open("Strip3_5.txt"); if (datafile) { for (index = 0; index <= 9; index++) { int input = datafile.parseInt(); String3_5[index] = input; Serial.println(input); } file.close(); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM