Components and supplies
1
Jumper wires (generic)
1
Infrared Module (Generic)
1
Arduino Nano R3
1
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
buzzer.ino
arduino
Comments
Only logged in users can leave comments
Components and supplies
Jumper wires (generic)
Infrared Module (Generic)
Arduino Nano R3
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
buzzer.ino
arduino
1// Define pins for ultrasonic and buzzer 2int const trigPin = 10; 3int const echoPin = 9; 4int const buzzPin = 2; 5void setup() 6{ 7pinMode(trigPin, OUTPUT); // trig pin will have pulses output 8pinMode(echoPin, INPUT); // echo pin should be input to get pulse width 9pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering 10} 11void loop() 12{ 13// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters 14int duration, distance; 15// Output pulse with 1ms width on trigPin 16digitalWrite(trigPin, HIGH); 17delay(1); 18digitalWrite(trigPin, LOW); 19// Measure the pulse input in echo pin 20duration = pulseIn(echoPin, HIGH); 21// Distance is half the duration devided by 29.1 (from datasheet) 22distance = (duration/2) / 29.1; 23// if distance less than 0.5 meter and more than 0 (0 or less means over range) 24if (distance <= 50 && distance >= 0) { 25// Buzz 26digitalWrite(buzzPin, HIGH); 27} else { 28// Don't buzz 29digitalWrite(buzzPin, LOW); 30} 31// Waiting 60 ms won't hurt any one 32delay(60); 33} 34
Comments
Only logged in users can leave comments