Page 198 - 15 Dangerously Mad Projects for the Evil Genius
P. 198
Chapter 13 ■ Levitation Machine 175
commands can be issued is then echoed to the the power we should apply to the coil. If the power
Serial Monitor. is outside the range for the analog output, then the
The “loop” function is automatically run next few lines “clip it.”
repeatedly. It is this loop that controls the coil. The “checkSerial” function checks for any
First, we define some static variables that will incoming commands from the Serial Monitor and
retain their value between calls of the function. processes them. If the m command is issued, then
The variable “count” is incremented each time the “monitoring” variable is set. Back in the “loop”
“loop” is run; “oldPosition” is used to find the function, if “monitoring” is true, then the position
change in position between samples, and hence the and speed will be written to the Serial Monitor.
While this is happening, it will slow everything
velocity; “ambient” is used to contain the ambient
down, likely causing levitation to fail.
light intensity when the LED is off; and, finally,
“powerCount” is used to keep track of how long Finally, we write the power level to the analog
the coil has been on and whether anything is being output controlling the power, and then delay for a
levitated or not. period of time determined by parameter D before
beginning the whole process again.
The “if” statement allows the code inside it to
be run just one time in every thousand. The first
thing it does is measure the ambient light—that is,
Summary
the light reading you get when the LED is off. This
is then used to adjust the light reading so the This is one of the easier projects in the book to
levitation device is not affected by changes to the build, but one of the most difficult to get right. It
lighting when a light is turned on. This happens will probably take some experimentation on your
because the “readAmbient” function momentarily part to make it all work. But the first time you get
turns off the LED and takes a reading of the light something to levitate, it all becomes more than
before turning the LED back on again. worthwhile.
The next part of the one-in-a-thousand block Feel free to experiment with levitating other
sets the “objectPresent” variable depending on the items—even items without a magnet in them. To
accumulated “powerCount.” This is important do this, however, you will need to move the sensor
because it allows the coil to be turned off when closer to the electromagnet.
there is no object being levitated. If we did not do
In the remaining two chapters of this book, we
this, then maximum power would be flowing
switch our attention to two very different kinds of
through the coil whenever there was nothing being
robots. Next stop: a tiny light-seeking robot that
levitated. This would make the coil dangerously
uses toothbrush motors.
hot after a while.
After the “if” statement, we do the calculations
to find the position and velocity, and then calculate