Challenge

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

const int echo = 7; // Echo Pin of Ultrasonic Sensor
#define size 5
int value=0,sum=0,avaraged=0,index=0,Readings[size];

void setup() {
Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
long duration, inches, cm;
pinMode(echo, OUTPUT);
digitalWrite(echo, LOW);
delayMicroseconds(2);
digitalWrite(echo, HIGH);
delayMicroseconds(10);
digitalWrite(echo, LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);
sum=sum-Readings[index];
//value=analogRead(echo);
Readings[index]=duration;
// index=(index+1)/size;
avaraged=sum/size;
cm=duration*0.034/2;
inches=cm/2.54;
Serial.print(avaraged);
Serial.print("avaraged, ");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}

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.

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.