Hardware Reference
In-Depth Information
Try replacing the part of your code
that prints the results with the code
to the right.
8
// print the results:
Serial.write(leftValue);
Serial.write(44);
Serial.write(rightValue);
Serial.write(44);
Serial.write(reset);
Serial.write(44);
// print the last sensor value with a println() so that
// each set of four readings prints on a line by itself:
Serial.write(10);
Serial.write(13);
Serial.write(serve);
When you view the results in the Serial
Monitor or terminal, you'll get some-
thing that looks like this:
.,P,,
(,F,,
(,A,,
),I,,
Before you go to the next section, where you'll
be writing some Processing code to interpret
the output of this program, you must undo this change.
!
What's going on? The original example uses
the Serial.print() command, which displays
the values of the sensors as their ASCII code
values: this modification sends out the raw binary values
using Serial.write() . The Serial Monitor and serial terminal
applications assume that every byte they receive is an
ASCII character, so they display the ASCII characters
corresponding to the raw binary values in the second
example. For example, the values 13 and 10 correspond
to the ASCII return and newline characters, respec-
tively. The value 44 corresponds to the ASCII comma
character. Those are the bytes you're sending in between
the sensor readings in the second example. The sensor
variables ( leftValue , rightValue , reset , and serve ) are the
source of the mystery characters. In the third line of the
output, when the second sensor's value is 65, you see the
character 'A' because the ASCII character 'A' has the value
65. For a complete list of the ASCII values corresponding
to each character, see www.asciitable.com .
Which way should you format your sensor values: as
raw binary or as ASCII? It depends on the capabilities of
the system that's receiving the data, and of those that
are passing it through. When you're writing software on
a personal computer, it's often easier for your software
to interpret raw values. However, many of the network
What's ASCII?
ASCII is the American Symbolic Code for Information Inter-
change. The scheme was created in 1967 by the American
Standards Association (now ANSI) as a means for all
computers, regardless of their operating systems, to be
able to exchange text-based messages. In ASCII, each letter,
numeral, or punctuation mark in the Roman alphabet is
assigned a number. Anything an end user types is converted
to a string of numbers, transmitted, and then reconverted
on the other end. In addition to letters, numbers, and punc-
tuation marks, certain page-formatting characters—like the
linefeed and carriage return (ASCII 10 and 13, respectively)—
have ASCII values. That way, not only the text of a the display
format of the message could be transmitted along with the
text. These are referred to as control characters , and they
take up the first 32 values in the ASCII set (ASCII 0 31).
All of the numbers, letters, punctuation, and control char-
acters are covered by 128 possible values. However, ASCII
is too limited to display non-English characters, and its few
control characters don't offer enough control in the age of
graphic user interfaces. Unicode—a more comprehensive
code that's a superset of ASCII—has replaced ASCII as the
standard for text interchange, and markup languages like
PostScript and HTML have replaced ASCII's page formatting,
but the original ASCII code still lingers on.
Search WWH ::




Custom Search