You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
664 B
29 lines
664 B
#define trigPin A1
|
|
#define echoPin A0
|
|
|
|
void setup() {
|
|
Serial.begin (9600);
|
|
pinMode(trigPin, OUTPUT);
|
|
pinMode(echoPin, INPUT);
|
|
}
|
|
|
|
void loop() {
|
|
long duration, distance;
|
|
digitalWrite(trigPin, LOW); // Added this line
|
|
delayMicroseconds(2); // Added this line
|
|
digitalWrite(trigPin, HIGH);
|
|
// delayMicroseconds(1000); - Removed this line
|
|
delayMicroseconds(10); // Added this line
|
|
digitalWrite(trigPin, LOW);
|
|
duration = pulseIn(echoPin, HIGH);
|
|
distance = (duration/2) / 29.1;
|
|
|
|
if (distance >= 1000 || distance <= 0) {
|
|
Serial.println("Out of range");
|
|
}
|
|
else {
|
|
Serial.print(distance);
|
|
Serial.println(" cm");
|
|
}
|
|
delay(250);
|
|
}
|
|
|