- Published on
Level 5 Solutions
Table of Contents
Assignment 1
Calculate the velocity of an object moving with constant velocity using the HC SR 04 Sensor. Assume that the velocity vector of the object points directly towards or away from the HC SR04 transmitter/receiver. (Ignore Doppler Effect)
//Set variables to 0
float dist,dist1,speed = 0;
//funtion to read distance
long readDistance(int echoPin, int trigPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(10, INPUT);
pinMode(11, OUTPUT);
}
void loop()
{ //velocity is change in distance/time
dist = 0.01715 * readDistance(10, 11); //distance
Serial.println(dist);
delay(1000);
dist1 = 0.01715 * readDistance(10, 11); //new distance
Serial.println(dist1);
speed=((dist1-dist)); //speed calculation
Serial.print("Velocity is: ");
Serial.print(speed);
Serial.println("cm/s");
}
Try to measure the velocity yourself.
Assignment 2
Use this velocity calculated in previous question to calculate the distance of the object after a given time interval. Measure the distance using the HC SR 04 sensor and calculate the error. (Ignore Doppler Effect)
This is an additon to previous code. Add the following to it.
delay((timeInterval-1)*1000); //wait for (total interval - 1 second)
dist2 = 0.01723 * readDistance(10, 11); //latest distance
Serial.print("Expected position: ");
Serial.println((dist+(speed*timeInterval))); //expected position
Serial.print("Final position: ")
Serial.println(dist2);
Serial.print("Error is: ");
Serial.println(dist2-(dist+(speed*timeInterval))); //Calculate error in distance
You can test the code here.
-->