Make Electronics

(nextflipdebug2) #1
What Next? 315

Experiment 36: The Lock, Revisited

Figure 5-148. This screenshot shows the complete
listing of a program to read a sequence of three
keypresses in conjunction with a combination lock.
If the sequence is correct, the PICAXE sends a high
output from one of its pins. If the sequence is incor-
rect, the program loops back to the beginning.

FundAmentAls


Limitations of MCUs


The PICAXE does have some disadvantages. Its voltage
requirements alone restrict you from using it with the kind
of freedom of a 555 timer.


Also, although I can get an instant result by plugging a 555
timer into a breadboard and adding a couple of resistors
and a couple of capacitors, the PICAXE requires me to add
a download socket, hook it up to my computer, write a
program in the Programming Editor, and download the
program.


Some people don’t like writing software, or they have dif-
ficulty thinking in the relentlessly left-brain way that com-
puter programming requires. They may prefer the hands-on
process of assembling hardware.


Other people may have the opposite preference. This of
course is a matter of taste, but one thing we know beyond
all doubt is that computer programs often contain errors
that may not reveal themselves until weeks or months later.


The PICAXE, for instance, doesn’t protect you if a number
is assigned to a variable that exceeds the limit for that type
of variable. Suppose b1=200 and b2=60 and your program
tells the PICAXE:
let b3 = b1 + b2
The result should be 260, but byte-size variables can only
count up to 255. What happens? You will find that b3 ac-
quires a value of 4, without any warning or explanation. This
is known as an “overflow error,” which can be very difficult
to predict, because it happens at runtime, when external
factors are in control. The code looks perfectly good; the
Programming Editor doesn’t find any syntax errors; the
simulation behaves properly. But in the real world, days or
even months later, an unexpected set of circumstances re-
sults in an input that causes the overflow, and because the
code is residing inside the chip at this point, you may have a
hard time figuring out what on earth went wrong.
Software has its problems. Hardware has its advantages.
Free download pdf