Challenge

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

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

//Master device
//Header to use LCD functions
#include<LiquidCrystal.h>
// Header to use I2C communication
#include<Wire.h>
//pin connected to LCD
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);

void setup()
{
lcd.begin(16,2);//(16,2)lcd clum and row
lcd.setCursor(0,0);
lcd.print("SKILL-LYNC");
lcd.setCursor(0,1);
lcd.print("I2C ARDUINO 2 ARDUINO");
for (int i=0;i<=16;i++)
{
lcd.scrollDisplayLeft();
delay(10);
}
delay(200);
lcd.clear();
Serial.begin(9600);
Wire.begin();// to begin I2C communication

}
void loop()
{
Wire.requestFrom(1,1);//function call by the slave 1
byte MR=Wire.read();//Read the slave 1 converted value
float inches=(MR/2.54);
// to move the cursor to the begining(when to start the display)
lcd.setCursor(0,0);
// to display in LCD
lcd.print("cm =");
lcd.print(MR);
lcd.print("cm");
//to move the cursor to the next line
lcd.setCursor(0,1);
lcd.print("inches =");
lcd.print(inches);
lcd.print("in");
Serial.print("Master Received from distance :");
Serial.print(MR);
Serial.print("cm\n");
Serial.print("inches =");
Serial.print(inches);
Serial.print("in\n");
//delay 1 sec
delay(600);
lcd.clear();
}

/Slave 1
int inches=0;
int cm=0;
int distance;
long duration;

#include<Wire.h>
#include <util/delay_basic.h>
void setup()
{
pinMode(9,OUTPUT);
Serial.begin(9600);
Wire.begin(1);// to begin I2C communication
Wire.onRequest(requestEvent);

}

void loop()
{
digitalWrite(9,LOW);
delayMicroseconds(2);
digitalWrite(9,HIGH);
delayMicroseconds(10);
digitalWrite(9,LOW);
duration=pulseIn(A0,HIGH);
}
void requestEvent()
{
//Reading analog data
//float echo = analogRead(A0);//read the temp output value the hold the volt
//Serial.print(echo);
cm=0.1723*duration;

byte distance = map(cm,0,1023,0,127);//10 bit to 8bit converstion
//writing converted data
Wire.write(distance);
Serial.println(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.