0% found this document useful (0 votes)
8 views2 pages

Hindusthan Project

This code uses an ultrasonic sensor and servo motor to measure distance to an object. If the distance is less than 50 units, the servo performs opening and closing movements. It averages multiple distance readings and includes libraries, global variables, setup, measurement, and main loop functions.

Uploaded by

Rock Rock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Hindusthan Project

This code uses an ultrasonic sensor and servo motor to measure distance to an object. If the distance is less than 50 units, the servo performs opening and closing movements. It averages multiple distance readings and includes libraries, global variables, setup, measurement, and main loop functions.

Uploaded by

Rock Rock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

This code is for an Arduino-based project that appears to be using an ultrasonic sensor (such as an HC-

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:

1. Libraries and Global Variables:

- The code begins by including the "Servo" library to control a servo motor.

- It defines some global variables for pins and measurements, including:

- `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).

- `led`: The pin number for an LED (digital pin 10).

- `duration`, `dist`: Variables to store the duration and distance measured by the ultrasonic sensor.

- `aver`: An array to store multiple distance measurements for averaging.

2. Setup Function (`setup`):

- The `setup` function initializes the serial communication at a baud rate of 9600 for debugging
purposes.

- It attaches the servo motor to the `servoPin`.

- It sets `trigPin` as an output and `echoPin` as an input.

- It moves the servo to an initial position (0 degrees) and waits for 100 milliseconds.

- It detaches the servo, so it doesn't consume power when not in use.

3. Measurement Function (`measure`):

- 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.

4. Main Loop Function (`loop`):

- 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.

- A short delay of 10 milliseconds is added between measurements.

- The average distance is calculated from the three measurements.

- If the average distance (`dist`) is less than 50 units, the servo motor is attached, and it performs a
series of movements:

- It attaches the servo to the `servoPin`.

- 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.

- Finally, it detaches the servo to save power.

- The measured distance is printed to the serial monitor for debugging.

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.

You might also like