2015-03-31 44 views
0

我一直在研究僞隨機生成命令句的程序。該程序結合了隨機選擇的動詞,所有格形容詞和名詞,以顯示最後一句。我最近一直試圖將Processing的代碼翻譯成Arduino,而今天我遇到了一些奇怪的行爲。這裏是我的代碼至今:Arduino:從SD卡中檢索字符時出現奇怪的行爲

#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」中的兩百個單詞時,通常會出現這個問題: char randSteps[3] = {200,1,200}; 即使我在「verb.txt」和「noun 。文本」。這與SRAM有關,與endOfLineNumber的大小有關,還是與某種緩衝區有關?

回答

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