//Muhammad Ariq Ramadhan 20200130157
float cm;
//float inches;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns
// the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
cm = 0.0344/2 * readUltrasonicDistance(3, 2);
//inches = (cm / 2.54);
//Serial.print("Inches ");
//Serial.print(inches, 1);
//Serial.print("\t");
Serial.print(cm, 1);
Serial.println("cm ");
delay(1000);
}