#include <pitches.h>
#define trigPin 9
#define echoPin 8
#define Buzzer1 5//active
#define Led1 6//Vibration
int sound = 250;
//int diso; // chec direction var
//int dise;
int de; // delay var
int f; // frequency
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Buzzer1, OUTPUT);
pinMode(Led1, OUTPUT);
}
void loop() {
Serial.begin(9600);
//for(int i=0;i>=10;i++){
long duration, distance;
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
digitalWrite(Buzzer1, LOW);
digitalWrite(Led1, LOW);
//if (i=5){
//diso=distance;
//}
//if (i=10){
// dise=distance;
//}
//if((dise-diso)<0){
if (distance<40) {
digitalWrite(Led1, HIGH);
tonecal(distance);
digitalWrite(Buzzer1, HIGH);
tone(Buzzer1, f); // Send 0.5KHz sound signal...
delay(de); // ...for 1 sec
noTone(Buzzer1); // Stop sound...
delay(de); // ...for 1sec
}
//}
// }
}
void tonecal(int d )
{
if (d>15){
de=30*d-300;
f=1050-(21.25*d+150);
}
if (d<=15){
de=20;
f=1000;
}
}