Challenge

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

#include

LiquidCrystal lcd = LiquidCrystal(10,9,8,7,6,5); // Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7):

const int pingPin = 12;

long time, distance;

void setup()

{

lcd.begin(16, 2); // Specify the LCD's number of columns and rows. Change to (20, 4) for a 20x4 LCD

Serial.begin(9600);

}

void loop()

{

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);

delayMicroseconds(2);

digitalWrite(pingPin, HIGH);

delayMicroseconds(5);

digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);

time = pulseIn(pingPin, HIGH);

Serial.print(time);

distance = (time*.0343)/2;

// For Serial Monitor

Serial.print("Distance:CM ");

Serial.println(distance);

// For LCD Display

lcd.setCursor(0,0);

lcd.print("Distance in CM");

lcd.setCursor(0,1);

lcd.print(distance);

}

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.