Challenge

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

SLAVE CODE ::

#include<Wire.h>//header for i2C communication
void setup()//setup function
{
  pinMode(12,OUTPUT);
  pinMode(11,INPUT);
  Wire.begin(1);//slave address
  Wire.onRequest(Request);//request from master
}

void loop()//infinite loop
{
}
void Request()//request function
{
  digitalWrite(12,LOW);//first to not to send any waves
  delayMicroseconds(2);//delay for microseconds
  digitalWrite(12,HIGH);//sending the wave
  delayMicroseconds(10);
  digitalWrite(12,LOW);//to stop sending the wave
  long cm=pulseIn(11,HIGH);//pulse in to get distance
  int c=(cm*0.01723);//converting the value in cm
  int inches=(c/2.54); //converting the value in inches
  Wire.write(inches);//writing value to master arduino
  Wire.write(c);//writing value to master arduino
}


MASTER CODE ::

#include<Wire.h>//header for i2C communication
#include <LiquidCrystal.h>//header file for lcd display
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);//enabling the pins to send send through the arduino to lcd
void setup()//setup function
{
  lcd_1.begin(16, 2); //to start the lcd communication
  Wire.begin();//begin the communication
}

void loop()//infinite loop
{
  Wire.requestFrom(1,2);//requesting from slave 
  byte b=Wire.read();//save it in variable b
  byte n=Wire.read();//save it in variable n
  lcd_1.setCursor(0,0);//setting the cursor in first column and first row
  lcd_1.print("DIS::");
  lcd_1.print(b);//printing in lcd
  lcd_1.print(" INCH AND");
  lcd_1.setCursor(0,1);
  lcd_1.print(n);//printing  the cm 
  lcd_1.print("CM");
  delay(100);//to delay to execute
}
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.