|
|
|
|
@ -1,4 +1,8 @@
|
|
|
|
|
// https://www.youtube.com/watch?v=M-UKXCUI0rE&list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP&index=53
|
|
|
|
|
// https://www.youtube.com/watch?v=BTMMNsL0_b0&list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP&index=54
|
|
|
|
|
// https://www.youtube.com/watch?v=2hwrDSVHQ-E&list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP&index=55
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h> |
|
|
|
|
@ -6,6 +10,9 @@
|
|
|
|
|
int trigPin = 12; |
|
|
|
|
int echoPin = 11; |
|
|
|
|
int pingTravleTime; |
|
|
|
|
float distance; |
|
|
|
|
const int pulsTime = 40; // uS
|
|
|
|
|
const float pingTimeToDistance = 0.017; // uS to cm, travletime[s] * 340 m/s / 2
|
|
|
|
|
|
|
|
|
|
void setup() { |
|
|
|
|
// put your setup code here, to run once:
|
|
|
|
|
@ -17,12 +24,17 @@ void setup() {
|
|
|
|
|
void loop() { |
|
|
|
|
// put your main code here, to run repeatedly:
|
|
|
|
|
digitalWrite(trigPin, LOW); |
|
|
|
|
delayMicroseconds(10); |
|
|
|
|
delayMicroseconds(pulsTime); |
|
|
|
|
digitalWrite(trigPin, HIGH); |
|
|
|
|
delayMicroseconds(10); |
|
|
|
|
delayMicroseconds(pulsTime); |
|
|
|
|
digitalWrite(trigPin, LOW); |
|
|
|
|
pingTravleTime = pulseIn(echoPin, HIGH); |
|
|
|
|
delay(25); |
|
|
|
|
Serial.print("Ping Time: "); |
|
|
|
|
Serial.println(pingTravleTime); |
|
|
|
|
Serial.print(pingTravleTime); |
|
|
|
|
|
|
|
|
|
distance = pingTravleTime * pingTimeToDistance; |
|
|
|
|
Serial.print(" Distance: "); |
|
|
|
|
Serial.print(distance); |
|
|
|
|
Serial.println(" cm"); |
|
|
|
|
} |
|
|
|
|
|