Skip to content

Instantly share code, notes, and snippets.

@atifahsuad
Created August 30, 2018 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifahsuad/3a645860332b537f8bb82ae20383bc3c to your computer and use it in GitHub Desktop.
Save atifahsuad/3a645860332b537f8bb82ae20383bc3c to your computer and use it in GitHub Desktop.
This example code is for Measuring Distance Using Ultrasonic Sensor's Tutorial.
/*
This example code is for Measuring Distance Using Ultrasonic Sensor.
Product page:
Maker UNO : https://www.cytron.io/p-maker-uno
Ultrasonic Sensor : https://www.cytron.io/p-sn-hc-sr04
Created by:
30/08/18 Suad Anwar, Cytron Technologies
*/
// defines pins numbers
const int buzzer = 8;
const int trigPin = 11;
const int echoPin = 12;
// defines variables
long duration;
int distance;
void setup() {
pinMode(buzzer, OUTPUT); // Sets the buzzer as an Output
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
digitalWrite(trigPin, LOW); // Clears the trigPin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration * 0.034 / 2; // Calculating the distance
Serial.print("Distance: "); // Prints the distance on the Serial Monitor
Serial.println(distance);
if(distance >7){
noTone;
Serial.print("Distance: "); // Prints the distance on the Serial Monitor
Serial.println(distance);
}
if(distance <=7 && distance >5){ // If the distance less and equal to 7 and the distance greater than 5
tone(buzzer,1000,200);
delay(700);
Serial.print("Distance: ");
Serial.println(distance);
}
if(distance <=5 && distance >3){ // If the distance less and equal to 5 and the distance greater than 3
tone(buzzer,1000,100);
delay(400);
Serial.print("Distance: ");
Serial.println(distance);
}
if(distance <=3){ // If the distance less and equal to 3
tone(buzzer,1000,100);
Serial.print("Distance: ");
Serial.println(distance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment