0

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

2 回答 2

0

您的整数如何保存到您的文件中?ASCII?或二进制。您的代码目前所做的是读取 8 位值,将它们发送到 UART/终端,然后读取终端并将一个字节保存到数组中。我不认为那是你想要的...

SD.read()

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

此外,这条线String3_5[index] = '\0';看起来更像是用于字符串的东西......

假设您的文件实际上是一个 ASCII 文件,您需要做的是实现读取 ASCII 数字、将它们转换为整数然后保存的代码。

于 2014-08-21T03:32:47.843 回答
0

这些代码应该是你正在寻找的

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

于 2015-04-08T09:07:57.493 回答