Created
January 22, 2016 22:27
-
-
Save tarunbod/1824d2169a455ea71c5a to your computer and use it in GitHub Desktop.
Arduino Ultrasonic Sensor Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define trigPin 11 | |
#define echoPin 12 | |
#define led 13 | |
long distance, duration, lastDistance; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(led, OUTPUT); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
} | |
void loop() { | |
lastDistance = distance; | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = duration / 58; | |
Serial.print("Distance: "); | |
Serial.print(distance); | |
Serial.println("cm"); | |
if (abs(distance - lastDistance) > 25) { | |
digitalWrite(led, HIGH); | |
tone(6, 1000); | |
} else { | |
digitalWrite(led, LOW); | |
noTone(6); | |
} | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment