Buzz Away
This project is a sensor that detects people or anything in front of it
Supplies
- Arduino
- Wires
- Two different colour LEDs
- Switch
- Distance sensor
- Buzzer
- Two 330 resistors
- Breadboard
Leds
Plug in the two LEDs to the side and plug in the resistors to the negative sides of the LEDs. Then plug the LEDs into pin 2 and 3
Distance Sensor
Then have the distance sensor on the other side of the LEDs and plug it in the positive on Vcc and negative is Gnd. then plug in trig into 10 and echo into 9
Buzzer
plug in the negative sides of the buzzer to the breadboard than the positive side into pin 13
Switch/power
use the switch as the positive charge and use another wire for the negative
Code
const int trigPin = 10;
const int echoPin = 9;
int soundPin =13;
float duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Serial.println("setup is complete");
pinMode (2,INPUT);
pinMode (3,INPUT);
pinMode (4,INPUT);
pinMode (soundPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.println(distance);
delay(100);
if ((distance > 20) && (distance < 100)) {
digitalWrite(3,HIGH);
} else {
digitalWrite(3,LOW);
}
if ((distance > 0) && (distance < 45)) {
digitalWrite(2,HIGH);
} else {
digitalWrite(2,LOW);
}
if ((distance > 0) && (distance < 40)) {
digitalWrite(soundPin,HIGH);
delay (100);
digitalWrite(soundPin,HIGH);
delay (100);
digitalWrite(soundPin,HIGH);
delay(90);
digitalWrite(soundPin,HIGH);
delay(90);
digitalWrite(soundPin,HIGH);
delay(90);
} else {
digitalWrite(soundPin,LOW);
}
}