|
@ -1,5 +1,5 @@ |
|
|
#define trigPin A1 |
|
|
#define trigPin 7 |
|
|
#define echoPin A0 |
|
|
#define echoPin 6 |
|
|
|
|
|
|
|
|
void setup() { |
|
|
void setup() { |
|
|
Serial.begin (9600); |
|
|
Serial.begin (9600); |
|
@ -9,21 +9,14 @@ void setup() { |
|
|
|
|
|
|
|
|
void loop() { |
|
|
void loop() { |
|
|
long duration, distance; |
|
|
long duration, distance; |
|
|
digitalWrite(trigPin, LOW); // Added this line
|
|
|
digitalWrite(trigPin, LOW); |
|
|
delayMicroseconds(2); // Added this line
|
|
|
delayMicroseconds(2); |
|
|
digitalWrite(trigPin, HIGH); |
|
|
digitalWrite(trigPin, HIGH); |
|
|
// delayMicroseconds(1000); - Removed this line
|
|
|
delayMicroseconds(10); |
|
|
delayMicroseconds(10); // Added this line
|
|
|
|
|
|
digitalWrite(trigPin, LOW); |
|
|
digitalWrite(trigPin, LOW); |
|
|
duration = pulseIn(echoPin, HIGH); |
|
|
duration = pulseIn(echoPin, HIGH); |
|
|
distance = (duration/2) / 29.1; |
|
|
distance = (duration/2) / 29.1; |
|
|
|
|
|
|
|
|
if (distance >= 1000 || distance <= 0) { |
|
|
|
|
|
Serial.println("Out of range"); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
Serial.print(distance); |
|
|
Serial.print(distance); |
|
|
Serial.println(" cm"); |
|
|
Serial.println(" cm"); |
|
|
} |
|
|
|
|
|
delay(250); |
|
|
delay(250); |
|
|
} |
|
|
} |
|
|