Alarma sonora y visual (con ultrasonido, buzzer y led RGB)
int cm = 0;
int buzzer = 11;
int rojo = 6;
int azul = 5;
int verde = 3;
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);
pinMode(rojo, OUTPUT);
pinMode(azul, OUTPUT);
pinMode(verde, 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
analogWrite(rojo, 255);
analogWrite(verde, 0);
}
else
{
digitalWrite(buzzer, LOW);
analogWrite(rojo, 0);
analogWrite(verde,255);
}
}
Comentarios
Publicar un comentario