int trigpin = 2;
int echopin = 3;
float distance = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(trigpin , OUTPUT);
pinMode(echopin , INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(distance,1);
Serial.println("10cm");
delay(50);
}
float getDistence() {
float echotime;
float calcualtedDistence;
digitalWrite(trigpin, HIGH);
delayMicroseconds( 10 );
digitalWrite(trigpin, LOW);
echotime = pulseIn(echopin, HIGH);
calcualtedDistence = echotime / 58.26;
return calcualtedDistence;
}