Alarma sonora (Buzzer con Ultrasonido)
int cm = 0;
int buzzer = 11;
long readUltrasonicDistance(int pin)
{
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin, and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}
void setup()
{
pinMode(7, INPUT);
Serial.begin(9600);
pinMode (buzzer, OUTPUT);
}
void loop()
{
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7);
if (cm < 70)
{
analogWrite(buzzer,128); //emite sonido
delay(500); //espera medio segundo
digitalWrite(buzzer, LOW); //deja de emitir
delay(500);//espera medio segundo
}
else
{
digitalWrite(buzzer, LOW);
}
}
Comentarios
Publicar un comentario