Hardware Reference
In-Depth Information
}
void loop()
{
val = analogRead(POT); //Read Pot
val = map(val, 0, 1023, 0, 179); //scale it to servo range
myServo.write(val); //sets the servo
delay(15); //waits for the servo
}
The include statement at the top of the program adds the functionality of
the servo library to your sketch. Servo myServo makes a servo object called
myServo . In your code, whenever you want to tell the servo what to do, you'll
refer to myServo . In setup() , attaching the servo initializes everything necessary
to control the servo. You can add multiple servos by calling the objects different
things and attaching a different pin to each one. In loop() , the pot is read, scaled
to an appropriate value for the servo control, and is then “written” to the servo
by pulsing the appropriate pin. The 15ms delay ensures that the servo reaches
its destination before you try to send it another command.
BuildingaSweepingDistanceSensor
To wrap up this chapter, you apply your knowledge from the past few chapters
to build a light-up sweeping distance sensor. The system consists of an infrared
(IR) distance sensor mounted on a servo motor and four LEDs. As the servo motor
cycles, it pans the distance sensor around the room, allowing you to roughly
determine where objects are close and where they are far. The four LEDs cor-
respond to four quadrants of the sweep and change brightness depending on
how close an object is in that quadrant.
Because IR light is a part of the electromagnetic spectrum that humans
cannot see, a system like this can be implemented to create “night vision.”
The IR distance sensor works by shining an IR LED and using some fairly
complex circuitry to calculate the angle at which that IR light returns to a
photo sensor mounted next to the IR LED. Using analog voltages created by
the IR photo sensor readings, the distance is calculated and converted to
an analog voltage signal that you can read into the microcontroller. Even if
the room is dark and you cannot see how close an object is, this sensor can
because it is using a wavelength of light that the human eye cannot detect.
Different models of IR rangefinders may have different interfaces. If you're
using a rangefinder that is different than the one used in this example, check
the datasheet to make sure it sends out a variable voltage as an output.
 
Search WWH ::




Custom Search