Hardware Reference
In-Depth Information
First, make sure that the drop-down is set to Newline. The drop-down menu
determines what, if anything, is appended to end of your commands when you
send them to the Arduino. The examples in the following sections assume that
you have Newline selected, which just appends a \n to the end of anything
that you send from the text entry field at the top of the serial monitor window.
Unlike with some other terminal programs, the Arduino IDE serial monitor
sends your whole command string at one time (at the baud rate you specify)
when you press the Enter key or the Send button. This is in contrast to other
serial terminals like PuTTy (linked from this chapter's digital content page at
www.exploringarduino.com ) that send characters as you type them.
ReadingInformationfromaComputerorOtherSerialDevice
You start by using the Arduino IDE serial monitor to send commands manually
to the Arduino. Once that's working, you'll learn how to send multiple com-
mand values at once and how to build a simple graphical interface for sending
commands.
It's important to recall that the Arduino's serial port has a buffer. In other
words, you can send several bytes of data at once and the Arduino will queue
them up and process them in order based on the content of your sketch. You
do not need to worry about sending data faster than your loop time, but you
do need to worry about sending so much data that it overflows the buffer and
information is lost.
Telling the Arduino to Echo Incoming Data
The simplest thing you can do is to have the Arduino echo back everything that
you send it. To accomplish this, the Arduino basically just needs to monitor its
serial input buffer and print any character that it receives. To do this, you need
to implement two new commands from the Serial object:
Serial.available() returns the number of characters (or bytes) that are
currently stored in the Arduino's incoming serial buffer. Whenever it's
more than zero, you will read the characters and echo them back to the
computer.
Serial.read() reads and returns the next character that is available in
the buffer.
Note that each call to Serial.read() will only return 1 byte, so you need to
run it for as long as Serial.available() is returning a value greater than zero.
Each time Serial.read() grabs a byte, that byte is removed from the buffer,
as well, so the next byte is ready to be read. With this knowledge, you can now
write and load the echo program in Listing 6-3 on to your Arduino.
Search WWH ::




Custom Search