EasyManua.ls Logo

SunFounder 3in1 Kit - Page 185

Default Icon
351 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder 3in1 Kit
After the code is uploaded successfully, you can enter text in the text box on the serial monitor, and the LCD will display
the information.
How it works?
void loop()
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}
Serial.available() can get the number of characters available in the incoming stream when you type some-
thing from the textbox. Since there are two terminators in the input, you actually have 3 characters when you
type A, and 4 characters when you type AB.
Serial.read() will take the first character from the incoming stream. For example, if you typed AB , calling
Serial.read() only once, will get the character A; The second call, you will get B; the third and fourth call,
you will get two end symbols; calling this function when the input stream has no characters available will result
in an error.
To sum up, it is common to combine the above two, using a while loop to read all characters entered each time.
while (Serial.available() > 0) {
Serial.print(Serial.read());
}
4.5. 5. More Syntax 181

Related product manuals