gpt4 book ai didi

c++ - Arduino Only 只读取一个字符。

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:09 25 4
gpt4 key购买 nike

该程序用于读取输入,并将其写入 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.

}

最佳答案

Serial.read 不等待字符串结束。如果您尝试读取几个字符(由 [4] 暗示),您只需要在 serial.read 之后包含一个简短的 delay() 以允许缓冲区有足够的时间完全转储字符串。

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

关于c++ - Arduino Only 只读取一个字符。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13687108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com