Stamp
looks simple on paper — with circles and dots — it takes a
bit of thinking. Let’s look at the code that determines
rotational direction and work through it:
Move_Spout:
IF (angle > pos) THEN
span = angle - pos
IF (span < HalfRev) THEN
stpDir = MCW
ELSE
stpDir = MCCW
span = RevSteps - span
ENDIF
ELSE
span = pos - angle
IF (span > HalfRev) THEN
stpDir = MCW
span = RevSteps - span
ELSE
stpDir = MCCW
ENDIF
ENDIF
Here we go: If the new angle is greater than the current
position, then we get the span by subtracting the old position from the new. Next, we check to see if that span is less
than half a revolution. If it is, then we set the stepper direction to clockwise; otherwise, it will rotate counterclockwise.
Notice that we have to make a correction in the span
if we determine that the shortest path is counter-clockwise.
If we don’t make this correction, the spout will overshoot
the desired position. We can use a similar set of logic when
the new angle is less than the current position — with
adjustments, of course, to make sure that we turn in the
direction that provides the shortest path.
Okay, let’s finish up and actually reposition the
stepper motor:
stpDelay = MoveTime / span
DO
stpIdx = stpIdx + stpDir // 4
READ (Steps + stpIdx), Coils
PAUSE stpDelay
span = span - 1
LOOP WHILE (span > 0)
pos = angle
RETURN
The rest of the subroutine starts by calculating the
delay between steps. The value is set by dividing the total
movement time (500 ms in our program) by the number
of steps in our move. Note that we must have some delay;
the stepper motor needs it. Also, we’ll want to experiment
with this a bit. The largest possible move is half a
revolution. Setting the time for that move to 500
milliseconds seems reasonable — fast enough to get there
swiftly, but not so fast as to sling coffee out of the spout.
A DO-LOOP is used to send the step data to the
SEPTEMBER 2004
motor. We’re back to our old tricks with the modulus operator; this allows us to set the direction with a variable. The
coil data is pulled from a DATA table with READ and
placed right on the pins. Of course, we cannot drive a stepper directly with BASIC Stamp IO pins; we need a high-current buffer to handle the coil load. Figure 3 shows how
to use our old friend, the ULN2003, to handle the load for
us. Finally, we need to update the pos variable with our
new position and start the whole process again.
Short and Sweet Rules!
Okay, I know this project wasn’t a big mental
challenge, but I do think it’s useful — even if we don’t
automate our car’s cup holder. I’m sure you’ll figure out
something fun to do with it and, when you do, don’t
hesitate to share your results with me.
Until next time, Happy Stamping. NV
Rejwil
wsJo
liam
Pa
wwon W
s@p
ralla
.paruillia
aral
x, I
allaxrms
lax.
nc
.cocom
.
m es
93