Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
“Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long time, distance;
void setup()
{
lcd.begin(16, 2); // Set up the number of columns and rows on the LCD.
pinMode(9, OUTPUT);
pinMode(10, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(9,LOW);
delay(100);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
// Read the time in milliseconds
time = pulseIn(10, HIGH);
// Calculate the distance
distance = time*0.034/2;
// set the cursor to column 0, line0
lcd.setCursor(0, 0);
// print the number of seconds since reset:
lcd.print(distance);
delay(1000); // Wait for 1000 millisecond(s)
}
