#define ULTRASONIC_TRIGGER_PIN 15
#define ULTRASONIC_ECHO_PIN 13
void setup_ultrasonic(){
pinMode(ULTRASONIC_TRIGGER_PIN, OUTPUT);
pinMode(ULTRASONIC_ECHO_PIN, INPUT);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
setup_ultrasonic();
}
int wizgear_ultrasonic_value(){
digitalWrite(ULTRASONIC_TRIGGER_PIN, LOW); // Clears the trigPin
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIGGER_PIN, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIGGER_PIN, LOW);
int dur = pulseIn(ULTRASONIC_ECHO_PIN, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
int dist = (int) dur*0.034/2; // Calculating the distance
return dist;
}
void loop() {
// put your main code here, to run repeatedly:
setup_ultrasonic();
int ultrasonicvalue = wizgear_ultrasonic_value();
Serial.print("Ultrasonic Value=");
Serial.println(ultrasonicvalue);
delay(1000);
}