Challenge

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

// C++ code

//

#include    //lcd display library

LiquidCrystal lcd(2, 7, 8, 9, 10, 11); 

const int trigPin = 3;//trigger pin set to pin no 3

const int echoPin = 4;//echo pin set to pin no 4

long duration;//duration of recieved signal from intrusion of object

int distancecm, distanceinch;//distance measured in centimeter and inches

//**************************//***********//*****************

void setup()

{

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  //*******************************************

  //*******************//***************//

  Serial.begin(9600); // Starts the serial communication

  lcd.begin(16,2);                           //Initilize LCD display

  lcd.setCursor(0,0);                        //Sets Cursor at first line of Display 

  lcd.print("****Welcome****");               

  lcd.setCursor(0,1);                        //Sets Cursor at second line of Display

  delay(1000);                               //Delay for 5 seconds

  lcd.clear(); 

  lcd.setCursor(0,0);

  lcd.print("********************");

  lcd.noBlink();

  delay(2000);

  lcd.blink();

  delay(2000);//Clears LCD display

  lcd.clear();

  lcd.setCursor(0,1);

  lcd.print("measuring distance");

   for (int positionCounter = 0; positionCounter < 13; positionCounter++) {

     // scroll one position left:

    lcd.scrollDisplayLeft();

     delay(20);

      lcd.scrollDisplayRight();

     delay(20);

     lcd.clear();

   }

}

void loop()

{

  digitalWrite(trigPin, LOW);//initially trigger pin is kept low

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);//making trigger pin high to send waves

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);// echo pin detects intrusion by recieving signal back after hitting object

  Serial.println(duration);

  distancecm= duration*0.034/2;//calculation to change duration from m/s to centimetrs

   distanceinch = duration*0.0133/2;//calculation to change duration from m/s to inches

  //****//making filter out higher frequencies*******//

  if(distanceinch <=125)

  {

   Serial.print("Distance: ");

    Serial.println(distancecm);

   Serial.println(distanceinch);

   lcd.clear();

   lcd.setCursor(0,0);

   lcd.print("Distance: ");

   lcd.print(distancecm);

   lcd.print(" cm"); 

   lcd.setCursor(0,1);

   lcd.print("Distance: ");

   lcd.print(distanceinch);

   lcd.print(" inch"); 

   delay(2000);

  }

   else

      {

       //Serial.println("Out of Range");

       lcd.setCursor(0,0);

       lcd.print("Out of range");

       delay(2000);

       lcd.clear();

       }

}

Result link: https://www.tinkercad.com/things/6dPpgGS3evc

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.