Arduino:从SD卡中检索字符时出现奇怪的行为

时间:2015-03-31 00:01:48

标签: char arduino sd-card

我一直致力于伪随机生成命令式句子的程序。该程序结合了随机选择的动词,所有格形容词和名词,以显示最终句子。我最近一直试图将代码从Processing转换为Arduino,今天我遇到了一些奇怪的行为。这是我到目前为止的代码:

#include <SD.h>

int ByteReceived;
int stepToLoad = 1;
int endOfLineNumber = 0;
int stringIndex = 0;
int fileNumber = 0;
File configFile;
char inputString [20];
char inputChar;
String file [3] = {
  "verb.txt","adject.txt","noun.txt"};

void setup() {
  Serial.begin(9600);
  char randSteps[3] = {
    random(1,1041),random(1,7),random(1,979)};

  pinMode(10, OUTPUT);
  SD.begin(10);

  for(fileNumber = 0; fileNumber < 3; fileNumber++){
    char filename[file[fileNumber].length()+1];
    file[fileNumber].toCharArray(filename, sizeof(filename));
    if(!SD.exists('\''+filename+'\''))
    {
      stepToLoad = randSteps[fileNumber];
      configFile = SD.open(filename);
      getStepData();
      Serial.print(inputString);
      Serial.print(" ");
      endOfLineNumber = 0;
      stringIndex = 0;
      inputString[0] = '\0';
      configFile.close();
    }
  }
}
void loop() {
  if (Serial.available() > 0)
  {
    ByteReceived = Serial.read();
    if(ByteReceived == '1')
    {
      setup();
    }
  }
}
void getStepData(){
  if (configFile) {
    while (configFile.available()) {  
      inputChar = configFile.read();
      if (inputChar != ' '){
        inputString[stringIndex] = inputChar;
        stringIndex++;
      } 
      else {
        endOfLineNumber++;
        if (endOfLineNumber == stepToLoad){
          inputString[stringIndex] = '\0';
          break;
        }
        else {
          stringIndex = 0;
        }    
      }
    }
  }
}    

我目前正在实施一些魔术数字来代替文本文件的最大字数: char randSteps[3] = {random(1,1041/*Total words in verb.txt*/),random(1,7/*Total words in adject.txt*/),random(1,979/*total words in noun.txt*/)}; 这只是为了在我调试代码时加快启动时间。 至于奇怪的行为,程序有时会打印一个完整的短语,如:

"abide my arithmetic"

其他时候它打印的内容如下:

"1oomylenddotde his 1oopergesstive"

此行为只会影响动词,只会影响名词,或同时影响两者。当它影响动词时,它总是读作“1oomylenddotde”,当它影响名词时,它会显示“1oopergesstive”。所有格形容词只有在我强制程序尝试读取文本文件中的单词总数时才会生效: char randSteps[3] = {1,9,1}; 形容词在这一点上写着“1heir”。但是,当程序试图在“verb.txt”或“noun.txt”上读取过去200个单词时,通常会出现此问题: char randSteps[3] = {200,1,200}; 即使我在“verb.txt”和“noun.txt”中有超过200个单词。这与SRAM有关,它与endOfLineNumber的大小有关,还是与某种缓冲区有关?

1 个答案:

答案 0 :(得分:0)

经过一些急需的休息后,我发现了这个问题。 我将随机整数存储为字符值。

char randSteps[3] = {
    random(1,1041),random(1,7),random(1,979)};

我现在已将其更改为:

int randSteps[3] = {
        random(1,1041),random(1,7),random(1,979)};
傻傻的我。这是工作代码:

#include <SD.h>

int ByteReceived;
int stepToLoad = 1;
int endOfLineNumber = 0;
int stringIndex = 0;
int fileNumber = 0;
File configFile;
char inputString [20];
char inputChar;
String file [3] = {
  "verb.txt","adject.txt","noun.txt"};

void setup() {
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  SD.begin(10);
  int randSteps[3] = {random(1,1041),random(1,7),random(1,979)};
  for(fileNumber = 0; fileNumber < 3; fileNumber++){
    char filename[file[fileNumber].length()+1];
    file[fileNumber].toCharArray(filename, sizeof(filename));
    if(!SD.exists('\''+filename+'\''))
    {
      stepToLoad = randSteps[fileNumber];
      configFile = SD.open(filename);
      getStepData();
      Serial.print(inputString);
      Serial.print(" ");
      stringIndex = 0;
      endOfLineNumber = 0;
      inputString[0] = '\0';
      configFile.close();
    }
  }
}
void loop() {
  if (Serial.available() > 0)
  {
    ByteReceived = Serial.read();
    if(ByteReceived == '1')
    {
      setup();
    }
  }
}
void getStepData(){
  if (configFile) {
    while (configFile.available()) {  
      inputChar = configFile.read();
      if (inputChar != ' '){
        inputString[stringIndex] = inputChar;
        stringIndex++;
      } 
      else {
        endOfLineNumber++;
        if (endOfLineNumber == stepToLoad){
          inputString[stringIndex] = '\0';
          break;
        }
        else {
          stringIndex = 0;
        }    
      }
    }
  }
}