end of the message to be emitted as Morse code. Finally, we set
MAX_MESSAGE_LEN
to the maximum length of the messages we are able to send.
Next we define three global variables:
message_text
is a character buffer that
gets filled with the data we receive on the serial port.
index
keeps track of our
current position in the buffer, and
telegraph
is the
Telegraph
object we’ll use to
convert a message into “blinkenlights.”
2
setup
initializes the serial port, and in
loop
we check whether new data has
arrived, calling
Serial.available
. We read the next byte if new data is available,
and we check whether it is a newline character or whether it is the last byte
that fits into our character buffer. In both cases, we set the last byte of
mes-
sage_text
to 0, because strings in C/C++ are null-terminated. We also reset
index
so we can read the next message, and finally we send the message using
our telegraph. In all other cases, we add the latest byte to the current message
text and move on.
You should compile and upload the program now. Open the serial monitor
and choose “Newline” from the line endings drop-down menu at the bottom
of the window. With this option set, the serial monitor will automatically
append a newline character to every line it sends to the Arduino. Enter a
message such as your name, click the Send button, and see how the Arduino
turns it into light. In the following figure, you can see what happens when
you enter my name.
2.
http://en.wikipedia.org/wiki/Blinkenlights
Chapter 4. Building a Morse Code Generator Library • 70
report erratum • discuss
www.it-ebooks.info