Challenge

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

code

int triggerPin = 7;
int echoPin = 6;
unsigned long duration;
int distance;

void setup()
{
pinMode(triggerPin, OUTPUT);
pinMode (echoPin, INPUT);
Serial.begin(9600);
}

void loop()
{
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
//clearing the trigger
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

// capturing the time duration for sound wave to travel in microseconds
duration = pulseIn(echoPin, HIGH);
distance = 0.01723 * duration;
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.