Jawaher.A.Fadhil
Ultrasonic With Buzzer
int buzzer = 8 ;
// Ultrasonic 4 pins
#define trigPin 12
// or (const int trigPin = 12;)
#define echoPin 13
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode (buzzer, OUTPUT) ;
}
void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
//delay(1000);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// speed=distance/time; speed of sound through air is = 29.1
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0)
{
Serial.println("Out of range");
noTone( buzzer );
}
else {
if (distance >= 10&& distance <=30 )
{tone( buzzer, 200 );
delayMicroseconds(10);
//delay(10);
//Serial.print(distance);
//Serial.println(" cm");
tone( buzzer, 200 );
//delayMicroseconds(10);
delay(300);
}
Serial.print(distance);
Serial.println(" cm");
noTone( buzzer );
delay(100);
}}

Ultrasonic with buzzer

  • 1.
    Jawaher.A.Fadhil Ultrasonic With Buzzer intbuzzer = 8 ; // Ultrasonic 4 pins #define trigPin 12 // or (const int trigPin = 12;) #define echoPin 13 void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode (buzzer, OUTPUT) ; } void loop() { int duration, distance; digitalWrite(trigPin, HIGH); //delay(1000); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); // speed=distance/time; speed of sound through air is = 29.1 distance = (duration/2) / 29.1; if (distance >= 200 || distance <= 0) { Serial.println("Out of range"); noTone( buzzer ); } else { if (distance >= 10&& distance <=30 ) {tone( buzzer, 200 ); delayMicroseconds(10); //delay(10); //Serial.print(distance); //Serial.println(" cm"); tone( buzzer, 200 ); //delayMicroseconds(10); delay(300); } Serial.print(distance); Serial.println(" cm"); noTone( buzzer ); delay(100); }}