简体   繁体   English

如何从Serial Read在Arduino LCD上存储两条分开的线

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

With my current code one character is read at a time, so the serial read of two separate lines is read as one large input. 使用我当前的代码,一次读取一个字符,因此将两行分开的串行读取作为一个大输入读取。 This causes both lines to be written on the same line of my LCD display on my Arduino. 这导致两条线都写在Arduino的LCD显示屏的同一行上。 Is there any way to have a null character or a break line to get these inputs to write on different lines? 有什么办法可以使用空字符或换行符来使这些输入写在不同的行上?

EDIT: Sorry, I should've specified that the input text will be of variable length. 编辑:对不起,我应该指定输入文本为可变长度。

Here is my Arduino code: 这是我的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);

  }
}

When the LCD is initialized, I'm guessing its default position would be 0,0 (ie first row and first column). 当LCD初始化时,我猜它的默认位置将是0,0(即第一行和第一列)。 Then for each character you read from the serial input, you print it to the LCD, and increase the column. 然后,对于从串行输入读取的每个字符,将其打印到LCD并增加列数。 If you get a newline in the input, then reset the LCD position to 1,0 (ie second row and first column). 如果输入中有换行符,则将LCD位置重置为1,0(即第二行和第一列)。 Continue reading and printing. 继续阅读和打印。


Example pseudo-code: 伪代码示例:

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

LCDs have a buffer for 1st line and for the second line at consecutive addresses, depending on LCD model (usually 40 or 64 characters per line) You could send a fixed number of characters for the first line right-padded with spaces then the second line. LCD在连续的地址上有第一行和第二行的缓冲区,具体取决于LCD型号(每行通常40或64个字符)。您可以为第一行发送固定数量的字符,并在其后右加空格。 example: First line<30 spaces>Second line 示例:第一行<30个空格>第二行

You might also need to set LCD (lcd.begin) not to scroll the display) 您可能还需要将LCD(lcd.begin)设置为不滚动显示)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM