Hardware Reference
In-Depth Information
int dist4 = 0; //Quadrant 4 Distance
void setup()
{
myServo.attach(SERVO); //Attach the Servo
pinMode(LED1, OUTPUT); //Set LED to Output
pinMode(LED2, OUTPUT); //Set LED to Output
pinMode(LED3, OUTPUT); //Set LED to Output
pinMode(LED4, OUTPUT); //Set LED to Output
}
void loop()
{
//Sweep the Servo into 4 regions and change the LEDs
dist1 = readDistance(15); //Measure IR Distance at 15 degrees
analogWrite(LED1, dist1); //Adjust LED Brightness
delay(300); //delay before next measurement
dist2 = readDistance(65); //Measure IR Distance at 65 degrees
analogWrite(LED2, dist2); //Adjust LED Brightness
delay(300); //delay before next measurement
dist3 = readDistance(115); //Measure IR Distance at 115 degrees
analogWrite(LED3, dist3); //Adjust LED Brightness
delay(300); //delay before next measurement
dist4 = readDistance(165); //Measure IR Distance at 165 degrees
analogWrite(LED4, dist4); //Adjust LED Brightness
delay(300); //delay before next measurement
}
int readDistance(int pos)
{
myServo.write(pos); //Move to given position
delay(600); //Wait for Servo to move
int dist = analogRead(IR); //Read IR Sensor
dist = map(dist, 50, 500, 0, 255); //scale it to LED range
dist = constrain(dist, 0, 255); //Constrain it
return dist; //Return scaled distance
}
The program employs a simple function that rotates the servo to the requested
degree, takes the distance measurement, scales it, and then returns it to the
loop() . Which map you choose for the LED range depends on the setup of your
system. I found that the closest object I wanted to detect registered around 500 ,
and the farthest object was around 50 , so the map() was set accordingly. loop()
executes this function for each of the four LEDs, then repeats. When complete,
your system should function similarly to the one shown in the demo video
listed at the beginning of this section.
Search WWH ::




Custom Search