can you tell me help to build a cicuit to do it?...i can't understend where i have to put the BUZZ_PIN...the code was that and is for an alarm:
#define TRIG_PIN 10
#define ECHO_PIN 9
#define BUZZ_PIN 8
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
digitalWrite(TRIG_PIN, LOW);
}
void loop() {
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG_PIN, LOW);
unsigned long tempo = pulseIn(ECHO_PIN, HIGH);
float distanza = 0.03438 * tempo / 2;
Serial.print("distanza: ");
Serial.print(distanza);
Serial.println("cm");
delay(20);
if(distanza <= 40)
{
digitalWrite(BUZZ_PIN, HIGH);
}
else
{
digitalWrite(BUZZ_PIN, LOW);
}
}