All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
CODE: CODE: const int samples = 5; // Number of measurements to take and averageint data[samples]; // Array to store the measurementsint index = 0; // Index to keep track of the current measurementint total = 0; // Running total of all measurementsint average = 0; // Calculated average distanceint duration = 0; // Duration…
Kotesh Mogudala
updated on 16 Apr 2023
CODE:
CODE:
const int samples = 5; // Number of measurements to take and average
int data[samples]; // Array to store the measurements
int index = 0; // Index to keep track of the current measurement
int total = 0; // Running total of all measurements
int average = 0; // Calculated average distance
int duration = 0; // Duration of the pulse
int inch = 0; // Distance in inch
const int inputPin = 4; // Pin connected to the ultrasonic sensor
void setup() {
Serial.begin(9600); // Start serial communication
for (int i = 0; i < samples; i++) {
data[i] = 0; // Initialize all elements in the data array to 0
}
}
void loop() {
total = total - data[index]; // Subtract the oldest measurement from the total
pinMode(inputPin, OUTPUT); // Set the pin to output
digitalWrite(inputPin, LOW); // Send a low signal to trigger the sensor
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(inputPin, HIGH); // Send a high signal
delayMicroseconds(10); // Wait for 10 microseconds
digitalWrite(inputPin, LOW); // Send a low signal to end the pulse
pinMode(inputPin, INPUT); // Set the pin to input
duration = pulseIn(inputPin, HIGH); // Measure the duration of the pulse
inch = (0.01723 * duration) / 2.54; // Calculate the distance in inch
data[index] = inch; // Store the measurement in the data array
total = total + data[index]; // Add the newest measurement to the total
index = index + 1; // Increment the index
if (index >= samples) {
index = 0; // Reset the index if all samples have been taken
}
average = total / samples; // Calculate the average distance
Serial.print("Object is ");
Serial.print(average); // Print the average distance
Serial.println("inches away!");
delay(100); // Wait for 100 milliseconds before taking another measurement
}
Output:
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Project 2
a) Max. heat generation of the battery pack is Heat generation (Wh) = *R*t.I = 0.896 AmpsR = 2m Ωt = 120 secs = 0.03333 hoursHeat generation (W sec) = *0.002*120 =…
24 May 2024 01:04 PM IST
Project 1
1. Design a battery pack for a car roughly 150 Kw with 120 V. Use 3500 mAh 3.6V nominal NMC chemistry cell.a. Design the battery pack configuration. b. Draw the BMS topology for this battery pack.a) Given, we have…
24 May 2024 12:40 PM IST
Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Live Batch (Preeti Mallikarjun):
01 Aug 2023 12:31 PM IST
Project 3
Live Batch ( Preeti Mallikarjun ):
22 May 2023 11:26 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.