#define trigPin 19
#define echPin 18
#define buzPin 5
#define button 15
bool pitar = false;
void setup() {
Serial.begin(9600);
//Serial.println("Hola mundo");
pinMode(trigPin, OUTPUT);
pinMode(echPin, INPUT);
digitalWrite(trigPin, LOW);
pinMode(button, INPUT);
}
void loop() {
int estadoButon = digitalRead(button);
if (estadoButon == HIGH) {
if(pitar == false){
pitar = true;
}else {
pitar = false;
}
}
long tiempo;
int distancia;
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
tiempo = pulseIn(echPin, HIGH); //obtenemos el ancho del pulso
distancia = tiempo * 0.034 * .5; //escalamos el tiempo a una distancia en cm
Serial.print("Distancia: ");
Serial.print(distancia); //Enviamos serialmente el valor de la distancia
Serial.print("cm");
Serial.println();
if (pitar == true) {
if (distancia >= 300) {
tone(buzPin, 50, 200);
tone(buzPin, 80, 200);
}
else if (distancia >= 200 && distancia < 300) {
tone(buzPin, 100, 200);
tone(buzPin, 120, 200);
}
else if (distancia >= 100 && distancia < 200) {
tone(buzPin, 120, 200);
tone(buzPin, 150, 200);
}
else if (distancia >= 50 && distancia < 100) {
tone(buzPin, 150, 200);
tone(buzPin, 200, 200);
}
else if (distancia >= 20 && distancia < 50) {
tone(buzPin, 200, 200);
tone(buzPin, 250, 200);
}
else if (distancia < 20) {
tone(buzPin, 260, 200);
tone(buzPin, 300, 200);
}
}
delay(1000);
}