#include <Ultrasonic.h>
Ultrasonic ultrasonic(11,12);
int d;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
d = ultrasonic.read();
//d = ultrasonic.Ranging(CM);
delay(100);
Serial.print("The distance is : ");
Serial.println(d);
delay(100);
}
// const int echoPin = 11; // Trigger pin
// const int triggerPin= 12; // Echo pin
// void setup() {
// Serial.begin(9600); // Start serial communication at 9600 bps
// pinMode(triggerPin, OUTPUT); // Set trigger pin as an OUTPUT
// pinMode(echoPin, INPUT); // Set echo pin as an INPUT
// }
// void loop() {
// long duration, distance;
// // Clear the trigger pin
// digitalWrite(triggerPin, LOW);
// delayMicroseconds(2);
// // Set the trigger pin HIGH for 10 microseconds
// digitalWrite(triggerPin, HIGH);
// delayMicroseconds(10);
// digitalWrite(triggerPin, LOW);
// // Read the echo pin, duration is the time it takes for the pulse to return
// duration = pulseIn(echoPin, HIGH);
// // Calculate the distance (duration/2) / 29.1 to convert to centimeters
// distance = (duration / 2) / 29.1;
// Serial.print("The distance is: ");
// Serial.print(distance);
// Serial.println(" cm");
// delay(100); // Wait for 1 second before the next reading
// }