Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Andrea S.
Published © CC BY

Burglar at the Door!

Detect when someone is standing or kneeling close to my apartment door for a suspicious time.

BeginnerFull instructions provided6 hours722
Burglar at the Door!

Things used in this project

Story

Read more

Code

Particle firmware (copy and paste in Particle IDE)

Arduino
You may want to adjust the variables at the beginning of the code to improve detection threshold
// -----------------------
// Burglar_PIR - Andrea S.
// 29/02/2020
// -----------------------

// Hardware
int pirPin = A5;
int blueLed = D7;
int buzzer = A2;                    // optional buzzer alarm
int debounceDelay = 100;            // signal must be high for at least 100 ms
// Motion detection
int countThreshold = 15;            // how many times must the PIR trigger to generate a detection
int decTime = 5000;                 // every 3 seconds the trigger count is decreased
int divider = 2;                    // how fast is the counter decreased after a detection
int motionCount = 0;                // number of triggers
int pirState = 0;
int ledTime = 150;                  // blink duration

// Timer to decrease trigger counts
Timer timerDec(decTime, decMotionCount);

void setup() {
    
    // Set pin modes
    pinMode(pirPin, INPUT);
    pinMode(blueLed, OUTPUT);
    pinMode(buzzer, OUTPUT);

    // Test tone
    tone(buzzer, 1800, 150);
    delay(200);
    tone(buzzer, 2500, 150);
  
    // Declare cloud variables
    Particle.variable("status", motionCount);
    // Start timer
    timerDec.start();
}

void loop() {
    // read the state of the PIR signal into a local variable:
    int reading = digitalRead(pirPin);
    
    // check to see if it is a true new detection:
    if (reading == HIGH) {
        delay(debounceDelay);
        reading = digitalRead(pirPin);
    }
    else {
        // PIR is not triggered
        pirState = LOW;
    }
    
    // trigger detected
    if (reading == HIGH && pirState == LOW) {
        motionCount++;
        pirState = HIGH;
    }

    if (motionCount > countThreshold) {
        // motion detected!
        blinkBlue(2);
        motionDetected();
        motionCount = motionCount / divider;
    }
}

void motionDetected() {
    // publish event
    Particle.publish("burglarPIR_motion", "motionDetected", 60, PUBLIC);
    // optional alarm sound
    tone(buzzer, 2500, 150);
    delay(150);
    tone(buzzer, 2500, 150);
}

void blinkBlue(int times) {
    for (int i=0; i<times; i++) {
        digitalWrite(blueLed, HIGH);
        delay(200);
        digitalWrite(blueLed, LOW);
        delay(100);
    }
}

void decMotionCount() {
    if (motionCount > 0)
        motionCount--;
}

Credits

Andrea S.

Andrea S.

5 projects • 19 followers
Technology enthusiast, problem solver, creative thinker!

Comments