2012-12-03 47 views
1

該程序是爲了讀取輸入,並將其寫入Arduino的串行監視器中。它只在串行監視器中寫入一個字符的問題。Arduino只讀取一個字符。

void setup() 
{ 
Serial.begin(9600); //Set the serial monitor. 
lcd.begin(16, 2); //Set the LCD 
} 
// Alignment variables 
boolean left = true; //Set boolean left to true to begin to display text in middle of screen. 
boolean right = false; //Other possible align booleans set to false 
boolean select = false; 
//Text show/hide variables 
boolean show1 = true; //Both values set to true to display on start up. 
boolean show2 = true; 
//Serial input 
char serialinput [4] = {0}; //For 3 value input, and null character to end. 
char line1; 
void loop() 
{ 

if (Serial.available() > 0) { //If the serial monitor is open it will read a value. 
    line1 = Serial.read(); 
    Serial.print(line1); 
    memmove (serialinput, &serialinput[1], 3); //copy the value to memory 
    serialinput [2] = Serial.read(); //value is read. 
    //if statements for each possible input. 

} 
+1

既然'line1'是一個'char',你怎麼期望它處理多個字符呢? –

回答

0

Serial.read不會等待字符串的結尾。如果你想讀取幾個字符([4]所暗示的),你只需要在serial.read之後包含一個短延遲(),以便有足夠的時間讓緩衝區完全轉儲字符串。

if (Serial.available() > 0) { //If the serial monitor is open it will read a value. 
line1 = Serial.read(); 
delay(100); 
Serial.print(line1); 
0

您讀取一個字節到line1,然後您打印該字節。一條線應該打印在一個循環或類似的地方。另外這個評論:

if (Serial.available() > 0) { 
//If the serial monitor is open it will read a value. 

是不是真的。 Serial.available()返回當前在緩衝區中的字節數。因此,此行:serialinput[2] = Serial.read();正在調用read(),但未驗證是否有要讀取的內容。顯然在loop()的末尾有//if statements for each possible input.,這些有什麼作用?我懷疑沒有任何邏輯來斷言serialinput[]哪裏會出現一個特定的字節。發送超過2個字節的消息到您在此處發佈的代碼將覆蓋這些值。