Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Q.)
ANS-
int trigpin=11
int ecopin=12
long duration, cm,inches;
long sum=0
long avg;
inti;
void setup()
{
//serial port begain
serial begain (9600);
pinmode(trigpin,output);
pin mode(echopin,input);
}
void loop
{
//The sensor is trigred by aHigh pluse of 10 or more microsecond.
//Give a short Low plusebrforehand to ensure a clean High pluse.
digiotal write(trig pin,LOW);
delay microsecond(10);
digitalwrite (trigpin LOW);
pinmode(echopin input);
duration=plusen(echopin high);
cm= (duration/2)29.1
inches=(duration/2)/74;
for(i=0,i<5,i++);
{
sum=sum+inches;
avg=sum/5;
}
serial.print(avg);
serial.print("avg");
serial.print(cm);
serial.print("cm")'
serial.print.n();
serial.print(inches);
serial.print("in");
serial.print();
delay(250);
}
circuit diagram
