int trigPin = 9;
int echoPin = 8;
long duraction, cm;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
digitalWrite(echoPin, HIGH);
delayMicroseconds(10);
digitalWrite(echoPin, LOW);
duraction = pulseIn(echoPin, HIGH);
cm = (duraction/2) / 29.1;
Serial.print(cm); // выводим расстояние в сантиметрах
Serial.println(" cm");
delay(1000); // ставим паузу в 1 секунду
}