Challenge

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

#define echo 2
#define trig 3

short duration;
int distance;

void setup()
{
  pinMode(trig,INPUT);
  pinMode(echo,OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  digitalWrite(trig,HIGH);
  delayMicroseconds(2);
  digitalWrite(trig,LOW);
  delayMicroseconds(10);
  digitalWrite(trig,HIGH);
  distance = duration*0.034/2;
  Serial.print(distance);
  Serial.println(" cm ");
}

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.