Skip to content

Instantly share code, notes, and snippets.

@tuankiet65
Created June 24, 2015 04:53
Show Gist options
  • Save tuankiet65/b82d68ea45f8209fd817 to your computer and use it in GitHub Desktop.
Save tuankiet65/b82d68ea45f8209fd817 to your computer and use it in GitHub Desktop.
#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