diff --git a/Learning_to_use_sensor.ino b/Learning_to_use_sensor.ino new file mode 100644 index 0000000..4cf7100 --- /dev/null +++ b/Learning_to_use_sensor.ino @@ -0,0 +1,33 @@ +#define trigPin 9 +#define echoPin 10 + +void setup() { + Serial.begin (9600); + pinMode(trigPin, OUTPUT); + pinMode(echoPin, INPUT); +} + +void loop() { + float duration, distance; + digitalWrite(trigPin, LOW); + delayMicroseconds(2); + + digitalWrite(trigPin, HIGH); + delayMicroseconds(10); + digitalWrite(trigPin, LOW); + + duration = pulseIn(echoPin, HIGH); + distance = (duration / 2) * 0.0344; + + if (distance >= 400 || distance <= 2){ + Serial.print("Distance = "); + Serial.println("Out of range"); + } + else { + Serial.print("Distance = "); + Serial.print(distance); + Serial.println(" cm"); + delay(500); + } + delay(500); +}