There are a few things here that need explaining. First, there is
Serial.available()
. To
understand this, you first need to know a little about how an Arduino communicates with
your computer over USB.
Figure 5-4
summarizes this process.
In the situation where the computer is sending data from
the Serial Monitor to the
Arduino board, then the USB is converted from the USB signal
levels and protocol to
something that the microcontroller on the Arduino board can use. This conversion happens
in a special-purpose chip on the Arduino board. The data is then received by a part of the
microcontroller called the Universal Asynchronous Receiver/Transmitter (UART). The
UART places the data it receives into a buffer. The buffer is a special area of memory (128
bytes) that can hold data that is removed as soon as it is read.
Figure 5-4
Serial communication with an Arduino
This communication happens regardless of what your sketch is doing. So, even though
you may be merrily flashing LEDs, data will still arrive in the buffer and sit there until you
are ready to read it. You can think of the buffer as being a bit like an e-mail inbox.
The way that you check to see whether you “have mail”
is to use the function
Dostları ilə paylaş: