The whole project is about measuring the distance between the sensor and an object in front of it. The goal is to check if a person passes through the area where the sensor is at. A super simple way to check if an area is trespassed ;)
To start with you need to run the code below:
const unsigned int TRIG_PIN=12;
const unsigned int ECHO_PIN=12;
const unsigned int BAUD_RATE=9600;
void loop () {}
void setup() {
Serial.begin(BAUD_RATE);
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
pinMode(ECHO_PIN, INPUT);
const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
int distance= duration/29/2;
if(duration==0){
Serial.println("Warning: no pulse from sensor");
}
else{
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm\n");
}
delay(100);
}
So as to count the distance once and to show you how much it is. From then on you can use this in a loop:
const unsigned int TRIG_PIN=12;
const unsigned int ECHO_PIN=12;
const unsigned int BAUD_RATE=9600;
void setup() {}
void loop () {
Serial.begin(BAUD_RATE);
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
pinMode(ECHO_PIN, INPUT);
const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
int distance= duration/29/2;
if(duration==0){
Serial.println("Warning: no pulse from sensor");
}
if(distance <50){
Serial.println("Warning: Somebody has breached the bunker!");
}
else{
Serial.print("You are safe. For now...\n");
}
delay(2000);
}
So as to check if somebody is in a hallway/doorway every 2 seconds. The amount of time between the loops can be changed via the:
delay(2000);
All done!
Comments