Hardware Reference
In-Depth Information
IR distance
sensor
Decoupling
capacitors
Battery
Servo
5V regulator
LEDs
Figure 4-14: Sweeping distance sensor wiring diagram
The last step is to program the sensor. The system works in the following
manner: Rotate to a given position, measure the distance, convert it to a value
that can be used for the LED, change that LED's brightness, move to the next
position, and so on, and so forth. Listing 4-5 shows the code to accomplish this.
Copy it into a new sketch and upload it to your Arduino.
Listing 4-5: Sweeping Distance Sensor—sweep.ino
//Sweeping Distance Sensor
#include <Servo.h>
const int SERVO =9; //Servo on Pin 9
const int IR =0; //IR Distance Sensor on Analog Pin 0
const int LED1 =3; //LED Output 1
const int LED2 =5; //LED Output 2
const int LED3 =6; //LED Output 3
const int LED4 =11; //LED Output 4
Servo myServo; //Servo Object
int dist1 = 0; //Quadrant 1 Distance
int dist2 = 0; //Quadrant 2 Distance
int dist3 = 0; //Quadrant 3 Distance
Search WWH ::




Custom Search