Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Char Arduino:从SD卡检索字符时的奇怪行为_Char_Arduino_Sd Card - Fatal编程技术网

Char Arduino:从SD卡检索字符时的奇怪行为

Char Arduino:从SD卡检索字符时的奇怪行为,char,arduino,sd-card,Char,Arduino,Sd Card,我一直在做一个伪随机生成祈使句的程序。该程序结合随机选择的动词、所有格形容词和名词来显示最后一个句子。我最近一直在尝试将代码从处理转换到Arduino,今天我遇到了一些奇怪的行为。以下是我迄今为止的代码: #include <SD.h> int ByteReceived; int stepToLoad = 1; int endOfLineNumber = 0; int stringIndex = 0; int fileNumber = 0; File configFile; cha

我一直在做一个伪随机生成祈使句的程序。该程序结合随机选择的动词、所有格形容词和名词来显示最后一个句子。我最近一直在尝试将代码从处理转换到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;
        }    
      }
    }
  }
}    
其他时候,它会打印如下内容:

"1oomylenddotde his 1oopergesstive"
这种行为可能只影响动词、名词或两者。当它作用于动词时,它总是读作1 omylenddode,当它作用于名词时,它读作1 operatesstive。所有格形容词只有在我强制程序尝试读取文本文件中的总字数后才会生效: char-randSteps[3]={1,9,1}; 此时形容词读作“继承人”。但是,当程序试图在verb.txt或noun.txt上读取过去200个单词时,通常会出现问题: char-randSteps[3]={200,1200};
尽管我在verb.txt和noun.txt中有200多个单词。这与SRAM有关,与Enumber的大小有关,还是与某种缓冲区有关?

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

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;
        }    
      }
    }
  }
}
#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;
        }    
      }
    }
  }
}