Devices & Components
Arduino Uno Rev3
Buzzer
Breadboard (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
Project description
Code
distance alarm
arduino
An alarm that measures the distance between the wall and door nob or door.
1const int TrigPin = 2; 2const int EchoPin = 3; 3const int Data = 8; 4float cm; 5void setup() 6{ 7 Serial.begin(9600); 8 pinMode(TrigPin, OUTPUT); 9 pinMode(EchoPin, INPUT); 10 pinMode(Data,OUTPUT); 11} 12void loop() 13{ 14 digitalWrite(TrigPin, LOW); 15 delayMicroseconds(2); 16 digitalWrite(TrigPin, HIGH); 17 delayMicroseconds(10); 18 digitalWrite(TrigPin, LOW); 19 cm = pulseIn(EchoPin, HIGH) / 58.0; //The echo time is converted into cm 20 cm = (int(cm * 100.0)) / 100.0; //Keep two decimal places 21 Serial.println(); 22 if(cm<15){ //if you are puting the sensor behind the door use ">" sysmbole insted of ">" p.s. make sure tat the door nob is less than 15 cm away 23 digitalWrite(Data,HIGH); 24 delay(200); 25 Serial.print("Door closed"); 26 } 27 else 28 { 29 digitalWrite(Data,LOW); 30 delay(200); 31 Serial.print("Door open"); 32 } 33 delay(200); 34} 35
Downloadable files
distance warnig
distance warnig
distance warnig
distance warnig
Comments
Only logged in users can leave comments