簡體   English   中英

如何從Serial Read在Arduino LCD上存儲兩條分開的線

[英]How to store two seperate lines on Arduino LCD from Serial Read

使用我當前的代碼,一次讀取一個字符,因此將兩行分開的串行讀取作為一個大輸入讀取。 這導致兩條線都寫在Arduino的LCD顯示屏的同一行上。 有什么辦法可以使用空字符或換行符來使這些輸入寫在不同的行上?

編輯:對不起,我應該指定輸入文本為可變長度。

這是我的Arduino代碼:

     #include <LiquidCrystal.h>
     #include <string.h>

    // These are the pins our LCD uses.
    LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
    // initalize the lcd, and button input at zero.
    int lcd_key     = 0;
    int adc_key_in  = 0;

    void setup()
    {
     Serial.begin(9600); //Set the serial monitor.
     lcd.begin(16, 2); //Set the LCD
    }
    char line1;
    void loop()
   {

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

  }
}

當LCD初始化時,我猜它的默認位置將是0,0(即第一行和第一列)。 然后,對於從串行輸入讀取的每個字符,將其打印到LCD並增加列數。 如果輸入中有換行符,則將LCD位置重置為1,0(即第二行和第一列)。 繼續閱讀和打印。


偽代碼示例:

int current_line = 0;
int current_col = 0;

void loop(void)
{
    char ch = read_char_from_serial();

    if (ch == '\n')
    {
        current_line++;
        current_col = 0;
    }
    else
    {
        lcd_goto(current_line, current_col++);
        lcd_put_char(ch);
    }
}

LCD在連續的地址上有第一行和第二行的緩沖區,具體取決於LCD型號(每行通常40或64個字符)。您可以為第一行發送固定數量的字符,並在其后右加空格。 示例:第一行<30個空格>第二行

您可能還需要將LCD(lcd.begin)設置為不滾動顯示)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM