Stamp
Figure 3. Stepper Connections.
Main:
GOSUB Get_Memsic
DEBUG HOME,
SDEC xG, CLREOL, CR,
SDEC yG, CLREOL, CR,
CR,
DEC (xG ATN yG), CLREOL
PAUSE 200
GOTO Main
So, under simulated acceleration, we get
zero, under braking, 128, turning right, 192, and
turning left, 64. Let’s think about this for a
moment. Wouldn’t it be a bit more convenient if
the output matched the number of steps of our
stepper motor? It might be easier to deal with,
mentally, if the values increased in a clockwise
direction, as well.
I happened to grab a stepper that has a 3. 6
degree step; this means that there are 100 steps
in each revolution. Here’s how we can do it:
setup. We can simulate acceleration of the car by tilting
the front end of the sensor (pins 1 and 6) up. We can
simulate braking by tilting the front end of the sensor
down.
angle = 100 - ((xG ATN yG) */ 100) // 100
This may look a bit convoluted, but I promise there is
a reason for every bit of it — let’s work from the inside out.
By using */ (star-slash) with a parameter of 100, we scale
the output of ATN from 0–255 to 0– 99. Our next issue,
then, is reversing the direction so that values increase
going clockwise instead of counter-clockwise, as they do
now. By subtracting from 100, we’re able to do that — with
one tiny glitch: what was 0 ends up being 100. Okay, then,
we can use the modulus operator on the whole works and
now our output is 0 to 99.
Accelerating means we should point the coffee spout
toward 0 (front of car); when braking, it will point toward
50 (rear of car), to 25 when turning right, and, when
turning left, 75. Let’s polish off our main loop:
Main:
GOSUB Get_Memsic
angle = 100 - ((xG ATN yG) */ 100) // 100
IF (angle <> pos) THEN
GOSUB Move_Spout
ENDIF
GOTO Main
END
F
o
r
E
l
e
c
t
r
o
n
i
c
s
NUTS & VOLTS
E
v
e
r
y
t
h
i
n
g
92
This is as simple as it gets: we read the new angle and
compare it to the current spout position. If there’s a
difference, then we reposition the spout; otherwise, we go
read the accelerometer again.
Before we can actually turn the spout, we need to find
the shortest path between the current position (pos) and
the new position (at angle). It actually took me a few
minutes to figure out how to code this one cleanly. While it
SEPTEMBER 2004