Devices & Components
Arduino Uno Rev3
HY-SRF05
Breadboard (generic)
Jumper wires (generic)
Project description
Code
Code to measure the distance
c_cpp
1const unsigned int TRIG_PIN=12; 2const unsigned int ECHO_PIN=12; 3const unsigned int BAUD_RATE=9600; 4 5void loop () {} 6void setup() { 7 8 Serial.begin(BAUD_RATE); 9 pinMode(TRIG_PIN, OUTPUT); 10 digitalWrite(TRIG_PIN, LOW); 11 delayMicroseconds(2); 12 digitalWrite(TRIG_PIN, HIGH); 13 delayMicroseconds(10); 14 digitalWrite(TRIG_PIN, LOW); 15 delayMicroseconds(2); 16 17pinMode(ECHO_PIN, INPUT); 18const unsigned long duration= pulseIn(ECHO_PIN, HIGH); 19int distance= duration/29/2; 20 21if(duration==0){ 22 Serial.println("Warning: no pulse from sensor"); 23 } 24 else{ 25 Serial.print("Distance: "); 26 Serial.print(distance); 27 Serial.print(" cm\ 28"); 29 } 30delay(100); 31}
Code to measure the distance
c_cpp
1const unsigned int TRIG_PIN=12; 2const unsigned int ECHO_PIN=12; 3const unsigned int BAUD_RATE=9600; 4 5void loop () {} 6void setup() { 7 8 Serial.begin(BAUD_RATE); 9 pinMode(TRIG_PIN, OUTPUT); 10 digitalWrite(TRIG_PIN, LOW); 11 delayMicroseconds(2); 12 digitalWrite(TRIG_PIN, HIGH); 13 delayMicroseconds(10); 14 digitalWrite(TRIG_PIN, LOW); 15 delayMicroseconds(2); 16 17pinMode(ECHO_PIN, INPUT); 18const unsigned long duration= pulseIn(ECHO_PIN, HIGH); 19int distance= duration/29/2; 20 21if(duration==0){ 22 Serial.println("Warning: no pulse from sensor"); 23 } 24 else{ 25 Serial.print("Distance: "); 26 Serial.print(distance); 27 Serial.print(" cm\ 28"); 29 } 30delay(100); 31}
Code to keep track of the area
c_cpp
1const unsigned int TRIG_PIN=12; 2const unsigned int ECHO_PIN=12; 3const unsigned int BAUD_RATE=9600; 4 5void setup() {} 6void loop () { 7 8 Serial.begin(BAUD_RATE); 9 pinMode(TRIG_PIN, OUTPUT); 10 digitalWrite(TRIG_PIN, LOW); 11 delayMicroseconds(2); 12 digitalWrite(TRIG_PIN, HIGH); 13 delayMicroseconds(10); 14 digitalWrite(TRIG_PIN, LOW); 15 16 delayMicroseconds(2); 17 18pinMode(ECHO_PIN, INPUT); 19 const unsigned long duration= pulseIn(ECHO_PIN, HIGH); 20 int distance= duration/29/2; 21 if(duration==0){ 22 Serial.println("Warning: no pulse from sensor"); 23 } 24 if(distance <50){ 25 Serial.println("Warning: Somebody has breached the bunker!"); 26 } 27 else{ 28 Serial.print("You are safe. For now...\ 29"); 30 } 31 delay(2000); 32 }
Downloadable files
Single Pin Wiring
Single Pin Wiring

Single Pin Wiring
Single Pin Wiring

Comments
Only logged in users can leave comments