The main purpose of our first
experiment is just to test the
hardware setup, so we don’t need to
get into the details of the program. (If
you’re interested, you may want to
reread the December 2012 column.)
At this point, just download the
temp9700test.bas program to your
breadboard setup.
You should see the three values
(ADCval, tempC, and tempF)
updating every five seconds in the
terminal window. (Don’t forget, even
though there’s a total of 10 seconds
wait time in the main loop, we’re
running at 8 MHz, so the actual loop
time is about five seconds.)
If you gently squeeze the 9700A
between two fingers, you should also
see the readings increase a bit, and
slowly decrease when you remove
your fingers from the sensor. When
you’re sure your hardware setup is
functioning correctly, we’re ready to
move on to our next experiment,
which will send the ADC data from
the 08M2 to the Pi. However, before
we do that, let’s take a little break
and play with a few of the features of
the Python print() function.
Playing with Print()
The remainder of our experiments
this month will all include the use of
the Python3 print() function, so that
we can visually check that the Pi is
receiving the correct data from the
08M2. The print() function includes
many advanced features that make it
suitable for printing various reports
that require precise formatting. We
definitely won’t be covering the
majority of these features, but there
are a couple of them we do need to
discuss, and I think it will be easier if
we do that before we actually tackle
the remaining experiments this month.
It may be tempting to compare
the print() function with serial output
such as the PICAXE serout and sertxd
statements. However, there are
important differences between the
two operations. Serial output is
frequently used to send data from
one processor to another. In that
situation, we only want the data
transmitted (without any additional
characters, such as spaces). In
PICAXE BASIC, if we write serout
txPin, T9600_ 8, (hiByte,loByte), we
expect to be sending exactly two
bytes, and that’s — in fact — what
happens.
However, if we’re transmitting
something that’s going to be read by
a human (e.g., a sertxd transmission
to the terminal window), we need to
insert spaces (and punctuation
characters) to make the output
readable. In that case, we need to
write something like sertxd (tempC,”
“,tempF) so that the two values are
separated by a single space, and
therefore easily readable.
On the other hand, the output of
a Python3 print() function is almost
always intended to be read by a
human. So, by default, Python3
automatically includes a blank space
between each argument in the print()
function. In addition, it also prints a
“new line” character at the end of
the printed line, so that whatever we
print subsequently appears on a
separate line.
In order to clarify this, let’s take a
look at a few examples. Open the
printPlay.py program in idle3. You will
see two variable definitions (tempC
and tempF) and a numbered series of
print() functions — all of which have
been commented out. Each example
includes a final “empty” print
function, which is one way we can
print a blank line between each
example. As you read each of the
following explanations, un-comment
the corresponding functions in
printPlay.py, and run the program to
see the printed result:
1. The output is printed on one
line with a space between each
argument. However, sometimes we
don’t want a space between
arguments. For example, the standard
degree symbol is character 176 in
the extended ASCII table. If we
wanted to get fancy and use the
symbol rather than the word
“degrees,” we might try #2.
2. Of course, the phrase is again
printed on one line, with a space
between each argument. That’s
definitely not what we want. There
shouldn’t be a space between the
number and the degree symbol, so
we need a way to override the
default behavior of the print()
function. Actually, Python3 provides
multiple ways to accomplish our goal,
but I’m just going to mention the two
solutions that I have found to be the
simplest to understand and use.
3. First, the print() function
includes two optional arguments: sep
(the “separator” character) and end
(the “end” character). If we omit
either or both of these arguments
(which is what we have done so far),
sep defaults to a “space” character,
and end defaults to the “newline”
character.
4. If we include sep and/or end,
10 April 2014
Post comments on this article and find any associated files and/or downloads at
www.nutsvolts.com/index.php?/magazine/article/april2014_PICAXEPrimer.
; FIGURE 3. Pinout of Pi breadboard connector.