Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
/* Project Name: To Measuring the distance of an object using ultrasonic sensor
*
* Created on 14-11-2022
* Author: NOORUL AMEEN SHAIK MOHAMED
*
*/
#define Trig_PIN 3
#define Echo_PIN 2
int distance;
long duration;
float inch;
void setup()
{
Serial.begin(9600);
pinMode(Trig_PIN, OUTPUT);
pinMode(Echo_PIN, INPUT);
}
void loop()
{
digitalWrite(Trig_PIN, LOW); // First being Trig to LOW for 2 Microseconds
delayMicroseconds(2);
digitalWrite(Trig_PIN, HIGH); // Trig send pulse signal duration of 10 Microseconds
delayMicroseconds(10);
digitalWrite(Trig_PIN, LOW); // Then bring Trig PIN to LOW
// Receiving of Bounce back signal using ECHO pin, Keep this PIN HIGH till completly receiving of Signal
duration = pulseIn(Echo_PIN, HIGH);
distance = (duration * 0.034)/2;// Converting of Received signals into Distance
inch = distance / 2.54; // Convert CM to INCH
Serial.print("DISTANCE: ");
Serial.print(inch); // Display Inch Values in Serial Monitor
Serial.print(" inch ");
Serial.print(distance); // Display CM Values in Serial Monitor
Serial.println(" cm");
delay(1000);
}