/*
'DISTANCE SENSOR:
Vcc goes to 5V on arduino
Gnd goes to ground pin on arduino.
Trigger goes to pin 8 on the arduino.
Echo goes to pin 9 on the arduino.
BUZZER:
Positive leg goes to pin 12 on the arduino.
Negative leg goes to GND on arduino.
*/
int trigPin = 8;
int echoPin = 9;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(100);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
delay(50);
if (duration <= 600){
tone(12, 2000, 3000);
}
else{
noTone(12);
}
}
Comments