Created
June 24, 2015 04:53
-
-
Save tuankiet65/b82d68ea45f8209fd817 to your computer and use it in GitHub Desktop.
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 TRIGGER_PIN 2 | |
#define ECHO_PIN 3 | |
void ultrasound_init(){ | |
pinMode(TRIGGER_PIN, OUTPUT); | |
pinMode(ECHO_PIN, INPUT); | |
} | |
int ultrasound_read(){ | |
digitalWrite(TRIGGER_PIN, LOW); | |
delayMicroseconds(10); | |
digitalWrite(TRIGGER_PIN, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(TRIGGER_PIN, LOW); | |
return pulseIn(ECHO_PIN, HIGH)/58; | |
} | |
void setup(){ | |
ultrasound_init(); | |
Serial.begin(9600); | |
} | |
void loop(){ | |
Serial.print("Distance to closest object in cm:"); | |
Serial.println(ultrasound_read()); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment