Hindusthan Project
Hindusthan Project
SR04) and a servo motor to measure the distance to an object and perform an action based on that
distance. Here's a breakdown of the code:
- The code begins by including the "Servo" library to control a servo motor.
- `trigPin`: The trigger pin of the ultrasonic sensor (connected to digital pin 5).
- `echoPin`: The echo pin of the ultrasonic sensor (connected to digital pin 6).
- `servoPin`: The pin to which the servo motor is connected (digital pin 7).
- `duration`, `dist`: Variables to store the duration and distance measured by the ultrasonic sensor.
- The `setup` function initializes the serial communication at a baud rate of 9600 for debugging
purposes.
- It moves the servo to an initial position (0 degrees) and waits for 100 milliseconds.
- The `measure` function is responsible for measuring the distance using the ultrasonic sensor.
- It triggers the sensor by briefly setting `trigPin` high and then low.
- It measures the duration of the echo pulse using `pulseIn` and calculates the distance based on the
speed of sound.
- The measured distance is stored in the `dist` variable.
- In the `loop` function, there is a loop that iterates three times to obtain three distance measurements
and calculate their average.
- The `measure` function is called to measure the distance and store it in the `aver` array.
- If the average distance (`dist`) is less than 50 units, the servo motor is attached, and it performs a
series of movements:
- It moves the servo to 0 degrees (closed position) and waits for 3 seconds.
- It then moves the servo to 150 degrees and waits for 1 second.
In summary, this code sets up an ultrasonic sensor and a servo motor to measure the distance to an
object. If the distance is below 50 units, it opens and closes a "cap" using the servo motor for a specific
duration. The code also averages multiple distance measurements to reduce noise.