Challenge

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

//MASTER
//I2C Communication between two arduino

//library for I2C communication function
#include <Wire.h>
//library for LCD display
#include <LiquidCrystal.h>
//Define LCD module pins
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);

void setup()
{
//Begin I2C communication at A4,A5
Wire.begin();
//Begin Serial communication at 9600 band rate
Serial.begin(9600);
}

void loop()
{
//request 1 byte from slave arduino(2)
Wire.requestFrom(2,1);
//receive a byte from the slave arduino and store in masterReceived
byte MasterReceive = Wire.read();
//read analog value from value
float value = analogRead(A0);
//convert digital value (0 - 1023) to (0 - 127)
byte MasterSend = map (value,0,1023,0,127);
//start transmit to slave arduino
Wire.beginTransmission(2);
//send one byte converted value to slave
Wire.write(MasterSend);
//stop transmit
Wire.endTransmission();
//intialize LCD display
lcd_1.begin(16,2);
//set cursor at line one in LCD
lcd_1.setCursor(0, 0);
//print master ate LCD
lcd_1.print("MASTER ARDUINO");
//set cursor at line two in LCD
lcd_1.setCursor(0,1);
//print slave temperature ate LCD
lcd_1.print("SLAVE TEMP :");
//print master received in LCD received from slave
lcd_1.print(MasterReceive);
//print in serial monitor
Serial.println("Master Receieved From Slave");
//print the changing vlaue of temperature sensor
Serial.println(MasterReceive);
// Wait for 1000 millisecond(s)
delay(1000);
}

//SLAVE ARDUINO
//I2C Communication between two arduino


//library for I2C communication function
#include<Wire.h>
//library for LCD display
#include<LiquidCrystal.h>
//Define LCD module pins
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);
byte SlaveReceived ;

void setup()
{
//intialize LCD display
lcd_1.begin(16,2);
//Begin Serial communication at 9600 band rate
Serial.begin(9600);
//Begin I2C communication at A4,A5
Wire.begin(2);
//Function call when slave receives value from master
Wire.onReceive(receiveEvent);
//Function call when master request value from slave
Wire.onRequest(requestEvent);
}

void loop()
{
//intialize LCD display
lcd_1.begin(16,2);
//set cursor at line one in LCD
lcd_1.setCursor(0,0);
//print slave arduino ate LCD
lcd_1.print("SLVAE ARDUINO");
//set cursor at line two in LCD
lcd_1.setCursor(0,1);
//print slave temperature ate LCD
lcd_1.print("MASTER TEMP :");
//print slave received in LCD received from master
lcd_1.print(SlaveReceived);
//print in serial monitor
Serial.println("Slave Received From Master");
//print the changing vlaue of temperature sensor
Serial.println(SlaveReceived);
// Wait for 1000 millisecond(s)
delay(1000);
lcd_1.clear();
}
//This Function is called when slave receives value from master
void receiveEvent(int a)
{
//Used to read value received from master and store in variable slave received
SlaveReceived = Wire.read();
}
//This function is called when master wants value from slave
void requestEvent ()
{
//read analog value from value
float value = analogRead(A0);
//convert digital value (0 - 1023) to (0 - 127)
byte SlaveSend = map(value,0,1023,0,127);
//send one byte converted value to master
Wire.write(SlaveSend);
}

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.