Challenge

Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter

Answer=

Code=

#include <LiquidCrystal.h>
#define trigger A4
#define echo A5

LiquidCrystal lcd(2,3,4,5,6,7);
float time=0,distance=0;

void setup()

{
lcd.begin(16,2);

pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);

lcd.print("Distance Meter");
delay(5000);
}

void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
digitalWrite(trigger,HIGH);
digitalWrite(trigger,LOW);

time=pulseIn(echo,HIGH);

distance=time*340/20000;

lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
delay(500);
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.