Challenge

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

#include <LiquidCrystal.h> // includes the LiquidCrystal Library

LiquidCrystal lcd(1, 2, 3, 4, 5, 6); //// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int pingPin = 7;

long duration;

int distanceCm, distanceInch;

void setup() {

// initialize serial communication:

lcd.begin(16,2);

pinMode(pingPin, OUTPUT);

pinMode(pingPin, INPUT);

lcd.setCursor(4,0);

lcd.print("Ultrasonic");

lcd.setCursor(6,1);

lcd.print("Sensor");

delay(1000);

lcd.clear();

}

void loop()

{

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);

delayMicroseconds(2);

digitalWrite(pingPin, HIGH);

delayMicroseconds(5);

digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);

duration = pulseIn(pingPin, HIGH);

// convert the time into a distance

distanceCm= duration*0.034/2;

distanceInch = duration * 0.01330 / 2;

lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed

lcd.print("Distance: "); // Prints string "Distance" on the LCD

lcd.print(distanceCm); // Prints the distance value from the sensor

lcd.print(" cm");

delay(10);

lcd.setCursor(0,1);

lcd.print("Distance: ");

lcd.print(distanceInch);

lcd.print(" inch");

delay(10);

}

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.