we can set each one equal to any
character or string that we want.
Here, we just redefine sep. This
example may seem silly, but it clearly
demonstrates what happens when
we include an optional definition for
the sep argument.
It also should give you an idea of
the formatting that’s possible in
Python3 printing. (We haven’t yet
solved our degree symbol problem,
but we will before we’re done!)
5. Here’s another attempt to
remove the space between the
temperature and the degree symbol.
As you can see, it also doesn’t work!
6. This one does the job. We just
needed to manually include spaces
where we want them to appear.
7. Sometimes, a print() function
is too long to conveniently type on
one line. Here, we solve that
problem by defining the end
argument as the empty string. This
example also demonstrates how we
can switch between single quotes
and double quotes, so that we can
print an apostrophe in the text.
8. I mentioned earlier that I like
two different Python solutions to our
degree symbol problem. The second
approach involves something called
“string concatenation.” In case you’re
not familiar with that term, it’s simply
a way of combining two or more
strings (or characters) to make a
single longer string.
In Python, the “+” symbol is used
for two different purposes: addition
and concatenation. If we place a +
symbol between two numbers,
Python adds them (duh!), but if we
place the same symbol between two
strings (or a string and a character, or
two characters), Python concatenates
them. The examples in this section
are somewhat trivial, but they do
demonstrate how concatenation
9. Here, we use concatenation to
solve our degree symbol problem.
Note that we have to convert the
two temps from numbers to strings
for this to work. Also, note how we
can define end as the empty string to
get everything to print on one line.
Now that we’ve covered a few
details of the Python3 print()
function, we can move on to our
remaining experiments. As you
encounter the various print()
functions in the upcoming Python
programs, you may want to refer
back to the above examples for
clarification. Better yet, you may want
to try a few of your own!
Experiment 2: Simple
from PICAXE to Pi
If you look again at the listing for
the temp9700test.bas program, you
can see that a significant portion of
the code is devoted to the conversion
of the raw data; first to Centigrade,
and then to Fahrenheit. The
conversion code is the most
complicated portion of the program,
and it results from the PICAXE’s
inability to perform computations on
negative integers, fractions, or
decimal numbers. Fortunately, we’re
about to get some mathematical help.
Since the PICAXE is helping the Pi
to deal with analog inputs, the Pi is
going to return the favor, and help the
PICAXE with the math! In other words,
we’re going to send just the raw data
to the Pi and let it do the math.
First, take a look at the listing for
the PICAXE program for Experiment
2 (tempSerial ToPi.bas). As you can
see, it’s much simpler than the
program we used in Experiment 1.
The subroutines have been
completely eliminated; the main
do/loop is less complicated; and
there are fewer variables.
However, there is one small
complication. We need to include
two new variables: hiByte and loByte.
This is necessary because all serial
communication is byte-based. In
other words, we can’t simply send
the ADCval to the Pi because that’s
a16-bit word value; we need to send
the high byte and low byte of that
Since w0 is comprised of b1
(high byte) and b0 (low byte), we’ve
defined suitable names for the two
bytes so that we can serially transmit
them to the Pi.
Also, note that it may look like
we’re sending a single 16-bit value in
the sertxd statement, but the
inclusion of the “#” symbol in a
sertxd or serout statement actually
results in a 16-bit value being
transmitted as 16 separate bytes. We
could have used the same approach,
but it’s much easier to just send two
bytes and let the Pi reconstruct the
16-bit value of ADCval.
Now, let’s turn our attention to
tempSerialFromAx.py (which is the Pi
program for Experiment 2) and
discuss the try portion of the main
while loop. In the program listing,
some of the program comments are
numbered. The following comments
elaborate on the corresponding
1. As we discussed in the
previous Primer, the pySerial read()
function reads a single byte, and it’s a
blocking function by default which
means the program will remain at
that point until a serial data byte is
received. Of course, in a real world
program, that would be unacceptable
— we don’t want our program to
However, we’re just using this
approach in our first example to keep
things simple. Before we’re finished
this month, we’ll implement one way
to avoid that problem.
2. Here, we reconstruct the 16-
bit value of ADCval. As you can see,
it’s a simple task.
3. In the December 2012
column, we used this equation to
have a PICAXE processor convert the
value of ADCval to Centigrade:
tempC=( 2*ADCval–500)/10. This
time, however, the Pi is doing the
Negative numbers and/or
decimals are no problem for the Pi,
so we can further simplify the above
equation to tempC=0.2*ADCval– 50;
April 2014 11