This is only a preview of the January 1999 issue of Silicon Chip. You can view 34 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "High Voltage Megohm Tester":
Articles in this series:
Items relevant to "Getting Going With BASIC Stamp":
Items relevant to "A LED Bargraph Ammeter For Your Car":
Items relevant to "Keypad Engine Immobiliser":
Articles in this series:
Articles in this series:
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
Getting Going With
BASIC Stamp
In these days of 32-bit microprocessors handling millions of instructions
per second and running at speeds in the hundreds of MHz, why on earth
would anyone want to use a strange 14-bit processor which can only
handle BASIC programs no bigger than 256 bytes . . .
and screams along at just 2000 BASIC instructions per second?
The answer is simple: because you CAN! Or is it because YOU can!
To program most microprocessors
you need to be an expert in machine
language code. Not so with the BASIC
Stamp.
If you know BASIC, you should be
able to program it to do, well, whatever you want to (as long as you can
do it in 256 bytes!). Even if you don’t
know BASIC, it’s not too hard to learn.
You’d know what D-I-Y stands for,
of course. Now the BASIC Stamp lets
you T-I-Y (think it yourself) before
you D-I-Y.
But what is this BASIC Stamp,
anyway?
You’ve probably heard of PIC microcontrollers. Made by Microchip (USA)
and intended for control use, they’re
found in countless applications and
sold in the millions upon millions (bet
your computer mouse even has one in
it!). In normal applications, they’re
programmed once and that’s it.
As versatile as they are for original
equipment manufacturers, though,
PIC chips are not particularly useful
for hobbyists and experimenters; at
least not in their own right.
That’s where the BASIC Stamp
comes in. Back in 1993, Chip Gracey
(no kidding, his name is Chip!) of
the US company Parallax combined
a 16C56 PIC microcontroller with a
93LC56 electrically erasable PROM,
loaded in a BASIC interpreter, enabled 8 input/output ports and a few
other goodies... and the BASIC Stamp
was born.
The version of the BASIC Stamp 1
we are using is supplied as a self-contained module, housed on a tiny (36 x
10mm) PC board and terminated to a
row of 14 header pins at 0.1-inch spacing. These mate with either a standard
14-pin header socket or even half of a
by ROSS TESTER & BOB NICOL*
32 Silicon Chip
28-pin IC socket. Components are all
surface mount types which means it’s
just as well you don’t have to do any
assembly (no pun intended) on the
BASIC Stamp PC board.
The reason it is called the BASIC
Stamp is that the original design fitted
onto a PC board the size of a postage
stamp. Today it’s just a tiny bit larger
with a few more components.
Fig.1 shows the circuit diagram
of the BASIC Stamp. In fact, this is
slightly different to the model we are
using because ours also has an onboard 5V regulator, allowing supplies
of up to 15V or even higher. Because
of its very low drain (1-2mA, ignoring
input/output or I/O requirements),
it is eminently suitable for battery
operation (a 9V battery will last for
weeks). In real-world applications
the I/O current cannot be ignored, so
a 9VDC plugpack of a few hundred
milliamps or so is generally preferred.
Speaking of I/O, any of the eight
pins will source up to 20mA or sink
up to 25mA, with the proviso of a total
loading of 40mA (source) or 50mA
(sink). This makes the BASIC Stamp
capable of directly driving LEDs, piezo buzzers, speakers and even some
sensitive relays. With buffering, of
course, much more can be driven.
For programming, the BASIC Stamp
connects to the parallel port of any
IBM-compatible personal computer
running good, ol’fashioned DOS.
That’s right, you don’t need Windows
– 1, 2, 3, 95, 98, NT, 2000 or any other
derivative.
And when we say any PC, we mean
any PC – here’s a good reason to fire
up the old XT or 8088-based machine
that’s been languishing in the garage
for 10 years!
(Of course, if you want to use the
latest Pentium II 450, go right ahead.
Better still, we’ll swap you a perfectly
good XT and throw in a Sydney Harbour Bridge ...)
Sure, the BASIC Stamp will only
handle 256 bytes of programming
(around 80-100 instructions). But in
these days of long-winded and often
superfluous coding, that teaches you
to be efficient, even miserly! We mentioned before that it is programmed
in BASIC. That’s not strictly true;
it’s Parallax’s own version of BASIC
called PBASIC. The structure is much
the same except that lines of code
are not numbered, they’re labelled.
However, if you know good old garden
variety BASIC (in any of its forms) you
shouldn’t have too much difficulty
with PBASIC.
PBASIC has a suite of 33 commands. The usual BASIC ones such
as GOTO, FOR-NEXT, IF and so on
are there but there are a few new ones
necessary for the Stamp’s role as a
logic controller chip. Any command
works with any I/O pin.
If you don’t know BASIC, here’s
the perfect opportunity to learn. It’s
a lot simpler than trying to learn
machine language, C or other more
recent languages. (BASIC, including
a detailed manual, was supplied with
most computers up until about the
’386 days. If you don’t have a copy,
it can be picked up for a song or even
a few notes).
The BASIC Stamp PC Board
Having waxed eloquent about the
BASIC Stamp’s features, the way it
is supplied is rather inconvenient to
use, not to mention risky.
Taking the latter point first, the
BASIC Stamp sells for around $80
yet, as far as we can tell, contains no
reverse polarity protection (despite
the on-board regulator). Our first
reaction was how we would feel if
we’d just spent eighty bucks and saw
it disappear in a wisp of smoke . . .
As far as incovenience goes, you obviously have to be able to interface to
the microprocessor board – somehow.
In the past, we’ve seen a number of
designs in magazines using perforated
strip board. We shudder.
If you’re building up a $10
circuit, strip board might be good
enough but remember, the BASIC
Stamp is worth $80! To use strip
board you have to cut tracks and
it’s easy to miss one or more. It’s
also very easy to short between
tracks with copper swarf, especially using the old “big drill in
small hole” track-cutting routine.
No! Strip board is a definite
no-no as far as we are concerned –
especially for this type of project.
The only logical solution was to
design our own PC board which
would not only accommodate the
BASIC Stamp module, it would
also allow us to include some
other components which would
make the whole thing that much
more user-friendly. That includes
reverse polarity protection and
some on-board supply decoupling
Fig.1: a somewhat simplified circuit diagram of the BASIC Stamp, with the
or smoothing.
actual module shown bottom right, same size. IC1 is a PIC microprocessor,
Because of the previously-mennormally one-time-programmable but in this configuration receives its data from
tioned
limitations on the BASIC
IC2, a 256-byte EEPROM (electrically erasable, programmable, read-only memory).
Stamp I/O, we have included a
An on-board 4MHz ceramic resonator sets operating speed. The ‘‘brownout’’ circuit
ULN2003 buffer. This is a 7-way
shown top left automatically resets the device if the supply falls below 4V.
January 1999 33
The SILICON CHIP version of the
BASIC Stamp experimenter's kit. The
thicker lead ending in the DB-25 plug
connects to the parallel port on any
IBM-compatible PC. The thin lead
is for power – in this case a 9VDC
plugpack.
The close-up photo of the PC board
itself reveals a 16-way pin header
(which allows connection to all
BASIC Stamp pins), reverse-polarity
protection diode and power supply
decoupling capacitor, the BASIC
Stamp module (end-on in its 14-way
header socket), the ULN2003 buffer IC
and a variety of input sensor devices
and output devices – LDR, 10-turn
trimpot, piezo buzzer, LED and DPDT
relay. Also provided are plenty of I/O
pads for further experimentation.
open-collector Darlington driver
which has a maximum collector current of 350mA or 500mA (depending
on brand) for each buffer so it can
drive significantly larger loads. We’ve
also included a double pole change
over (DPDT) relay, driven by one of
the ULN2003 outputs. And before
anyone thinks we’ve forgotten the
usual suppression diode across the
relay contacts, each of the ULN2003
outputs has one built in.
Jumping ahead of ourselves, the
ULN2003 gets quite hot – no, very
o
hot. But it's rated at 150 C so there's
no great problem. Just thought we'd
warn you in advance!
There’s also a LED, an LDR, trimpot
and piezo buzzer mounted on the
board. These will be used in some
simple programs which you can use
to try out the BASIC Stamp – before
you start writing your own! Into the
bargain, we’ve also included some
spare tracks and pads which would
accommodate other components for
uses as yet undreamed of.
Putting it together
Assembly of the PC board is very
simple, as it should be with just a
handful of components. Start with the
PC stakes (the relay outputs) then the
Parts List for Timer Projects
1 BS1-IC BASIC Stamp module with
stamp1.exe operating software, PC
parallel port connection cable and
power connection cable (see text)
1 ULN2003 7-way peripheral driver
1 1N4004 or similar diode
1 LED, any type
1 1kΩ 1/4W resistor
1 20kΩ, 10-turn vertical trimpot
1 light-dependent resistor
1 100µF 25VW electrolytic capacitor
34 Silicon Chip
1 12V mini relay with DPDT contacts
1 small piezo buzzer
1 PC board, 103x55mm, coded
SC11301991
1 16-way header pin set, 0.1in spacing
1 14-way header socket, 0.1in spacing
3 (or 6) PC stakes (as required)
1 plugpack supply, approx 9-12V DC
<at> 400-500mA output
(to suit application)
resistor, LDR, LED and diode – their
pigtails will give you the wire you
need for the link and the two connection wires for the piezo buzzer.
It’s probably a good idea to fit the
socket terminal strip and pin header
next, before any of the larger components start crowding the board. The
socket can go either way around.
Ideally, the pin header and the
sockets on the power and output leads
should be marked some way so that
they will never be incorrectly connected. We used some bright red nail
polish to paint a red stripe on the base
of the third pin in from one end of the
header, and some quick-drying paint
to put a stripe on the next (fourth) pin.
We used yellow ’cause we had some.
We painted matching stripes on the
output lead socket (a red stripe on the
+ lead) and on the output lead socket
(a yellow stripe on the end of the
socket with the green wire attached.
In retrospect, green would have been
a better colour to use!). If you don’t
have any paint, try coloured correction fluid.
When later connecting the sockets
to the pin header, it’s just a matter of
connecting colour to colour.
Having marked the pin header in
this way, it must be mounted with the
marked pins closest to the diode (the
red pin, marking the positive supply,
actually lines up with the diode).
The pins are closely spaced, so
check very carefully between each pad
after soldering for dags shorting out
pads. If necessary, use a multimeter
or a magnifying glass.
Next the large components – the
relay, trimpot and electrolytic capacitor – can be soldered in place,
followed by the piezo buzzer. As the
photograph shows, this is mounted
edge-on to the PC board with the pins
connected to vertical wires soldered
to the appropriate PC pads.
After checking thoroughly, carefully insert the BASIC Stamp module into
the 14-way socket. Take care that all
pins actually go into the socket and
mate properly – it’s easy to bend them.
It’s also easy to insert the module backto-front: make sure the components
on the module face the input socket.
STAMP EXPERIMENTER'S BOARD
Figs.2 & 3: the circuit (above) and PC board layout (below) for BASIC
Stamp experiments and circuit development. With the components shown
either the simple or complex timer can be built but the large number of
spare pads make adapting this exclusive SILICON CHIP design very easy.
What else do you need?
Now that you’ve finished the BASIC
Stamp PC board you’re just itching to
get going, right? Whoa! It’s simple, but
not quite THAT simple. There are a
few more things you'll need.
First and foremost, you need
software that will allow your PC to
communicate with (and program) the
BASIC Stamp. For BASIC Stamp 1
(which we are using) the software is
stamp1.exe. You’ll also need connecting cables and, unless you’re already
a wizz at PBASIC and programming,
some form of driving instructions.
Fortunately, there’s an easy way
to get all this in one package. Dick
Smith Electronics stores stock the
BASIC Stamp Development kit which
contains all of the above for less than
$150. It also includes a certificate
entitling you to three months free
technical support.
The BASIC Stamp manual, by the
way, is more than 460 pages thick
so it’s no lightweight. It contains 23
application notes to try out and also
contains information on the higher
spec’d BASIC Stamp II.
Fig.4 (below): use this PC board layout to make your own board or to
check commercial boards. The pattern is also available for download (in
Adobe PDF format) from the SILICON CHIP website:
www.siliconchip.com.au
January 1999 35
Both these books from Dick Smith
Electronics will be invaluable for
anyone interested in the BASIC
Stamp.
A Few Stamp FAQs
Before we conclude our look at the
BASIC Stamp, we’ll try to answer a
few FAQs (for those not into webspeak, that stands for frequently asked
questions).
‘‘Programming and Customizing
the BASIC Stamp Controller’’ (left,
$77.95) has nearly 300 pages with
many BASIC Stamp projects to try.
It also includes a CD-ROM with a
variety of software tools and the
BASIC Stamp applications, along
with the stamp1.exe software.
“Can the program storage memory be increased?” No, the PBASIC
interpreter only addresses 8 bits
of program space, which results
in the 256-byte limitation. Using a
larger EEPROM won’t make any
difference. It is possible to use
external memory for storing lookup
tables and extra data – you’ll find
information on this in the BASIC
Stamp manual.
“Can the BASIC Stamp support
floating point maths?” No, it only
works with integer maths, which
means fractions are out. If your
program required the BASIC Stamp
to divide 7 by 2, it would give you
the answer 3, not 3.5.
“Speaking of maths, how does
the BASIC Stamp evaluate maths
expressions?” Strictly left to right
– not, as you might expect, following
maths conventions. For example, a
BASIC Stamp would evaluate 1+2x3
as 9, not 7. That is, it would work it
out as (1+2)x3, not 1+(2x3).
“What is the BASIC Stamp’s
life?” Hard to say! A program is
guaranteed to stay in memory for
40 years but there is a finite limit to
the number of times the EEPROM
can be reprogrammed. Fortunately,
that limit is about 10 million times so
you’re hardly likely to reach that in
a hurry. However, swapping files to
and from the EEPROM (in an effort
to overcome the 256-byte limitation)
could reach this limit much more
quickly, so this is not recommended.
“How do I get more than 25mA
output current from the I/O
lines?” You don’t – that’s one of
the quickest ways to blow up the processor. That is precisely the reason
we added the ULN2003 buffer: it can
sink up to 350mA collector current
per output (not 500mA as you might
see claimed). If you drive all seven
ULN2003 inputs gates from the BASIC Stamp I/O you'll still be under
the 40mA maximum output limit.
36 Silicon Chip
The ‘‘BASIC Stamp Manual’’
(below) is part of the BASIC Stamp
Development Kit ($149) which also
includes the cables and software.
If you already
have a working
knowledge of the
BASIC Stamp and
don’t want to buy
the Development
Kit, an alternative
would be to buy the
programming cable
(also available from
Dick Smith Electronics – Cat K-1407
<at> $19.95).
Another very
handy reference is
the Scott Edwards
book, “Programming and Customizing the BASIC
Stamp Computer”
(you guessed where
from – Cat B-4807 <at> $77.95). It contains a host of very detailed information, projects and even a CD-ROM of
software tools including the stamp1.
exe program.
Meanwhile, back at the ranch
...
Let’s assume you have the BASIC
Stamp board completed, the programming cable to connect to your PC and
copies of the experimental software.
Ah! Experimental software – we haven’t mentioned that yet, have we?
The experimental BASIC programs
(as distinct from the executable
stamp1.exe) are all listed overleaf.
Simply type them as a text file in any
word processor or text editor and save
them with the names shown.
But if you don't feel like doing all
that typing, don't! All listings are
available from the “Software Downloads” page on the SILICON CHIP website – www.siliconchip.com.au – and
the best part of all is they are free! So
before we get too much further down
the track type out the software or log
on to the website and download it.
There are four programs – adjust.
bas, test.bas, simple.bas and complex.
bas. All four files come to less than 6kB
so they’ll only take a few moments to
download. That is, unless you want to
explore the SILICON CHIP website while
you’re there!
OK, so we’re ready to go.
The first step is obvious: connect
the programming cable to the BASIC
Stamp and to your PC’s parallel port.
About now you’ll be starting to think
"Perhaps I should have put the paint
spots on those cables. Which way
around do they go?" Plug in power
and turn on the computer (the IBM
compatible, that is).
We mentioned a moment ago that
the software is tiny. Even stamp1.exe
is only 14kB. Therefore it is perfectly
practical to run the BASIC Stamp
software from a floppy-disc-only
computer. Yes, we really did mean
any IBM compatible!
If you have a hard disc, of course,
it’s preferable to run it from that, if
only because you’ll never misplace the
floppy disc. Create a directory called
STAMP1 and copy stamp1.exe and the
four BASIC programs into it. It makes
sense to include the STAMP1 directory in your DOS “path” statement,
especially if you’re going to be doing
a lot of experimenting with files.
Which ever you choose, floppy or
hard disc directory, go to your DOS
prompt and log on to that disc or
directory. Load the operating software for the BASIC Stamp 1 module,
stamp1.exe.
Once stamp1.exe is loaded, you
should have an almost blank, blue
screen as shown overleaf. Fear not!
You haven’t loaded one of Mr Gates’s
blue screens of death. Your next step
is to load the experimental BASIC
programs which you downloaded.
Just a reminder – these are just ideas
to get your creative juices flowing.
Once you’ve played with the BASIC
Stamp, you could come up with a
whole host of ideas!
Where to get help
The BASIC Stamp project described
here and the accompanying software
is, of necessity, very elementary – just
enough to whet your appetite to develop bigger and better applications using
this simple, yet clever little module.
Because of its simplicity, we imagine that very few constructors will
have any difficulties with this project
as published but that may not be the
case as you expand your horizons. So
where do you go when you need help?
If you buy the Dick Smith Electronics development kit, you will receive
a certificate entitling you to three
months software support through
the Australian distributors, MicroZed
Computers. This is available by phone
(02) 6772 2777; fax (02) 6772 8987
or email – support<at>microzed.com.
au Note that support is available
for STAMP product and software
obtained only through Dick Smith
Electronics or MicroZed.
You can also obtain information and
support through the Microzed web
site, www.microzed.com.au
A great deal more information and
backup support is available from the
Parallax website, www.parallaxinc.
com There’s even an evaluation copy
of stamp1.exe to help you decide if
you want to buy the real thing.
As mentioned before, if you don't
feel like typing them out the four
PBASIC programs – adjust.bas, test.
bas, simple.bas, and complex.bas – are
all available from www.siliconchip.
com.au; however technical support
is not available from this website nor
SC
from SILICON CHIP magazine.
* Bob Nicol manages MicroZed Computers,
authorised Australian distributors of Parallax
Inc's products.
OVERLEAF: HOW TO LOAD THE SOFTWARE PLUS COMPLETE PBASIC LISTINGS
January 1999 37
LOADING THE SOFTWARE
Loading BASIC Stamp software is a three-part process:
first load the application software (stamp1.exe) into your
IBM-compatible PC;
second load the PBASIC program (*.bas) from disc into your
PC;
third download it to the BASIC Stamp module.
Load stamp1.
exe from the
DOS prompt (it
doesn’t matter
whether you
do it from DOS
itself or from a
DOS Window.
Our screen
shots show the
latter). A nearly
blank blue
screen should appear as above.
Type ALT L
(ie, hold down
the Alt key and
touch the L key
at the same
time). This will
bring up a
menu of the
*.bas software
in your
STAMP1
directory. In
this case we are choosing adjust.bas.
Type ALT
R to run.The
program takes
about a second
(or less) to
download to the
BASIC Stamp
and if all is OK,
will run immediately. The
histogram
which appears
shows how much BASIC Stamp memory is taken from total
memory, the red or dark portion showing memory used.
Once loaded,
it stays until
another is
loaded. If there
are ‘‘debug’’
lines in your
PBASIC program they will
show up on the
first loading.
38 Silicon
ilicon C
Chip
hip
38 S
Setting the LDR – adjust.bas
’adjust.bas - LDR threshold setting - Silicon Chip January 1999
’This short program allows setting of the LDR to an appropriate level
’LED is on pin 6 via ULN2003 Solenoid driver chip
’LDR is on pin 5, adjust the potentiometer, when LDR is in light
’then hold finger over LDR adjust so that LED goes off
LOOP:
LOW 6
IF PIN5 = 1 THEN LED
GOTO LOOP
‘this segment looks at LDR
‘turn LED off
‘Goto subroutine named LED
‘go back to beginning, to look again
LED:
HIGH 6
‘this segment turns LED on
‘this line does just that
GOTO LOOP
‘go back to look at LDR again
First of all, load the “adjust.bas” program. This lets you set the
threshold of the LDR between light and dark. Now you can adjust
the trimpot. If the LED is on, turn the pot anticlockwise to the point
where it just turns off, then back again until it just turns on. Put
your finger over the LDR and the LED should turn off. Note that
this setting applies to the light levels in that room at that point – if
you change locations you might need to run adjust.bas again.
One point to keep in mind: the BASIC program loaded into EEPROM is going to stay there for a long, long time – it’s guaranteed
for 40 years, even with power removed. It will also keep operating
even if the PC which programmed it is turned off and/or disconnected. You can erase the contents of the EEPROM by loading
another program – which is exactly what we are going to do now.
The simple timer – simple.bas
’Simple.bas - Simple Timer - Silicon Chip January 1999
LDR:
’this segment looks at LDR
LOW 0
’turn relay off
LOW 6
IF PIN5 = 0 THEN RELAY ’Goto subroutine named RELAY
GOTO LDR
’go back to beginning, to look again
RELAY:
’this segment turns relay on
HIGH 0
’this line does the job
’
HIGH 6
’this line turns LED on too, to enable
’line remove first apostrophe in line
PAUSE 20000
’Stamp marks time for 20 seconds
GOTO LDR
’go back to look at LDR again
The simple timer is an example of a BASIC Stamp application
which may or may not be particularly useful – but it’s an interesting
example nevertheless.
Do you have a deep cupboard or storeroom which doesn’t have
a light of its own, or where the light can’t reach into the furthest
corners? This BASIC Stamp program might solve that problem.
It is designed to sit in a cupboard or room and sense when the
cupboard or storeroom door is opened, allowing some light in. The
LDR in the circuit senses the light and the microprocessor pulls in
a relay (via the interface chip).
This can then be used to turn on an additional light (battery operated?) in that dark corner. Overkill, when you can do the same thing
with an LDR and transistor? Probably. But these applications are not
intended to be so much practical as examples of what can be done
with the BASIC Stamp. You will have noticed that no details are given
for the connection to the light – that’s the easy part!
Once again, follow the “adjust.bas” steps to load the software.
Note that the additional light must not be in the field of view of the
LDR, otherwise the system becomes a closed loop and the additional
light will be locked on all the time. With the software as it stands, the
light will stay on for 15 minutes.
The BASIC Stamp test – test.bas
’Test.bas - test components on timer PCB - Silicon Chip January 1999
SOUND 7,(100,1000)
‘beep piezo sounder for 1 second
HIGH 0
‘relay on
HIGH 6
‘LED on
TEST_LOOP:
DEBUG “pin 5 getting logic “, #PIN5, “ from LDR”,CR
IF PIN5 = 0 THEN ALL_OFF ‘turn everything off
IF PIN5 = 1 THEN ALL_ON
‘turn everything on
GOTO TEST_LOOP
‘tedious, but let’s do it again
ALL_OFF:
‘this section of the program turns all devices off & beeps
LOW 6
‘LED off
LOW 0
‘relay off
SOUND 7,(100,10)
‘one beep each time around
GOTO TEST_LOOP
‘back to the tedious bit
ALL_ON:
DEBUG “ * * * * * * “, CR
‘separates DEBUG lines on screen
HIGH 6
‘LED on
HIGH 0
‘relay on
SOUND 7,(120,5,120,5)
‘two short beeps each time around
GOTO TEST_LOOP
‘back to the tedious bit
This little routine puts all of the on-board components through
their paces just to make sure everything is working properly. Apart
from that, it doesn’t do very much except demonstrate how the
program interacts with the hardware!
Loading the program is exactly the same as loading the adjust.
bas above, with the obvious exception of the program name!
What this will do is sound the piezo buzzer for 2 seconds, light
the LED, close the relay and pulse the piezo buzzer. It will stay in
that state until you block the light to the LDR, when the LED goes
out, the relay drops out and the buzzer tone drops and quickens.
The complex timer – complex.bas
(listing at right)
Aunty Maud has left you with her much loved but rather delicate pot plant with strict instructions to water it every evening.
Alas, you’ve forgotten it for the last three days and the pot plant
is looking, well, not well. Wouldn’t it be nice if you had another
brain to do it for you? BASIC Stamp has a brain!
With the complex timer software loaded it will sense dusk,
turn on the relay for a minute (or whatever other time you set
from about 1/1000 of a second (!) right up to a minute – or more
correctly, 65,535 milliseconds). That’s as the program is written
– but if you wanted to, you could add extra “pause” statements
and eventually drown the poor plant, the dog and the next door
neighbour’s oak tree.
The timer doesn’t trigger again until the next dusk. The program
takes into account that night time is just a bit longer than the normal
watering cycle of the system. And if you’re really clever in setting
the LDR threshold and writing code, it could even detect full cloud
cover and skip those days. It also takes into account daylight but
will ignore a torch or car headlights flashed on it.
It really does have a lot of features but there are many more
which could be incorporated – it just depends on how clever you
want to be at adapting the complex.bas program.
Exactly how you water Aunty Maud’s pot plant is left up to you
– there’s a relay output to turn on your ingenuity, we’re sure (or is
that a mixed metaphor?). A windscreen washer pump or a valve
connected to an overhead bucket are both ideas that spring to
mind. But the purpose of this software is not so much to demonstrate what to do but how it can be done and modified to suit your
requirements. Now, who's into hydroponics . . .
These program listings are also available for
downloading free of charge at the SILICON CHIP
website: www.siliconchip.com.au
COMPLEX.BAS - COMPLEX TIMER LISTING
’Program for Complex Timer - Silicon Chip January 1999
’by Bob Nicol, MicroZed Computers
’DEBUG statements are used in this program. They show on your PC screen the first time
'you load the program and NOT when program is run again.
’DEBUG statements should be disabled with an apostrophe, and the program loaded to the
'Stamp again when you have finished editing and checking the program to suit your needs
’pin assignments and settings, plus some startup fiddles:
’PIN 0 output to pin 7 of ULN2003 to drive relay on pin 10
LOW 0
’Make sure pin 0 is low
LOW 1
’PIN 1 Not used set low
LOW 2
’PIN 2 Not used set low
LOW 3
’PIN 3 Not used set low
LOW 4
’PIN 4 Not used set low
INPUT 5
’sets pin 5 as an input to accept LDR level
’PIN 6 output to pin 4 of ULN2003 to drive LED on pin 11 of ULN
HIGH 6
’turn LED on
PAUSE 1000
’leave LED on for 1 second
LOW 6
’Then turn LED off and make sure pin 6 is low
’PIN 7 output to piezo sounder
SOUND 7,(100,200)
’make a noise on start up
’Variables
’B2 accumulates number of times LDR (PIN 5) is low
’B3 accumulates number of times LDR (PIN 5) is high
’B4 accumulates count for reset B2
’W4 accumulates darkness events
’B5 Flags ACTION already done
WAITING_FOR_CHANGE:
’This program module keeps looking at LDR
’and initiates action when LDR is dark
’long enough to be a valid condition
DEBUG “PIN 5 IS “,#PIN5,CR
’shows if LDR on pin 5 is in dark(0) or light(1)
DEBUG “LDR HAS SEEN DARK “,#B2,” TIMES”,CR ’shows on PC screen DARK / LIGHT
DEBUG “LDR HAS SEEN LIGHT “,#B3,” TIMES”,CR ’counts in B2(DARK) & B3(LIGHT)
PAUSE 1000
’wait one second
IF PIN5 = 0 THEN INCREMENT_D
’goto increment_D to add 1 to B2
IF PIN5 = 1 THEN INCREMENT_L
’goto increment_L to 1 to B3
B4 = B4 + 1
’add 1 to B4
DEBUG “LDR has been looked at “,#B4,” times”,CR,CR
PAUSE 1000
’wait one second
IF B4 > 5 THEN DECREMENT
’B2 hasn’t increased, go to DECREMENT
’to reset B2, B3 & B4
GOTO WAITING_FOR_CHANGE
’keep going around this loop
INCREMENT_D:
’LDR has seen darkness
DEBUG “INCREMENT DARK B2 “,CR,CR
‘PC screen shows we are in increment
B2 = B2 +1
’B2 gets one more added to it
IF B5 = 1 THEN HOLDOFF
’intercept Action, already done
IF B2 > 10 THEN ACTION
’LDR has seen at least 10 Dark signals
GOTO WAITING_FOR_CHANGE
INCREMENT_L:
DEBUG “INCREMENT LIGHT B3”,CR,CR
B3 = B3 +1
IF B3 > 10 THEN RESET_B5
GOTO DECREMENT
DECREMENT:
DEBUG “DECREMENT”,CR
DEBUG “RESETING LDR STATUS”,CR,CR
B2 = 0
B4 = 0
GOTO WAITING_FOR_CHANGE
ACTION:
SOUND 7,(60,100,80,100,100,100,120,100)
DEBUG “ACTION, RELAY ON, LED ON”,CR,CR
HIGH 0
HIGH 6
PAUSE 2000
LOW 0
LOW 6
B2 = 0
W4 = W4+1
B5 = 1
DEBUG “Darkness has occured “,#W4,” times”,CR
GOTO WAITING_FOR_CHANGE
HOLDOFF:
DEBUG “ HOLD OFF UNTIL LIGHT SEEN”
B2 = 0
GOTO WAITING_FOR_CHANGE
RESET_B5:
DEBUG CR,CR,” R E S E T B5 & B3",CR,CR
B5 = 0
B3 = 0
GOTO WAITING_FOR_CHANGE
’get back to looking at LDR signal
’LDR has seen light
’B3 gets one more added to it
’seen enough light to make change
’reset dark and light variables
’there has been no valid darkness
’show on PC screen decrease happened
’set darkness count to zero
’set false counts to zero
’go back to looking at LDR signal
’Darkness criteria met, do something about it
’audible warning of action
’show on PC screen we are doing it
’turn relay on via ULN2003
’turn LED on via ULN2003
’stay that way for 2 seconds
’turn relay off
’turn LED off
’reset B2
’keep count of times turned on
’show on PC screen count of events
’go back to looking at LDR signal
’this is a loop to stop action already taken
’show on PC screen
’reset B2
’go back to keep looking at LDR
’Stop HOLDOFF cycling
’Show on PC screen
’when B5 is 1 HOLDOFF is used
’reset light counts
’go back and keep looking at LDR
ANUARY 1999 39
1999 39
January
|