Zachary HunnicuttJackson Stone
Published © GPL3+

Motion Activated Alarm System

Motion Activated Alarm System!

BeginnerFull instructions provided1,782
Motion Activated Alarm System

Things used in this project

Story

Read more

Schematics

PIR Setup

Buzzer and OLED Setup

Code

PIR Code

C/C++
int inputPin = D0;              // choose the input pin (for PIR sensor)
int ledPin = D1;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

int calibrateTime = 10000;      // wait for the calibration

void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     // declare sensor as input
}

void loop()
{

  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from the sensor
    readTheSensor();

    // report it out, if the state has changed
    reportTheData();
  }
}

void readTheSensor() {
  val = digitalRead(inputPin);
}

bool calibrated() {
  return millis() - calibrateTime > 0;
}

void reportTheData() {

  // if the sensor reads high
  // or there is now motion
  if (val == HIGH) {

    // the current state is no motion
    // i.e. it's just changed
    // announce this change by publishing an eent
    if (pirState == LOW) {
      // we have just turned on
      Particle.publish("designingiot/s15/motion");
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      setLED( pirState );
    }
  }
}

void setLED( int state )
{
  digitalWrite( ledPin, state );
}

Buzzer Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"
int intruders = 0;
int buzz = D0;
int butt = D3;
#define OLED_DC     D2
#define OLED_CS     D4
#define OLED_RESET  D5
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
 int  x, minX;
void setup() {
    pinMode(butt, INPUT_PULLUP);
    pinMode(buzz, OUTPUT);
    digitalWrite(buzz, LOW);
    Particle.subscribe("designingiot/s15/motion", myHandler, "33002c000947353138383138" );
display.begin(SSD1306_SWITCHCAPVCC);

  display.setTextSize(4);       // text size
  display.setTextColor(WHITE); // text color
  display.setTextWrap(false); // turn off text wrapping so we can do scrolling
  
   display.clearDisplay();
    display.setCursor(2, 2);
    display.print("SAFE");
    display.display();
 

}
void myHandler(const char *event, const char *data)
{
    
    
    if (data)
        digitalWrite(buzz, HIGH);
        intruders = intruders+1;
        Particle.publish("Intruders",String(intruders));
   
    
    
}


   


void loop() {
    
   
int pushButtonState;
 pushButtonState = digitalRead(butt);
 if(pushButtonState == LOW)
        digitalWrite(buzz, LOW);  
    else
     Serial.println("NULL");
     
int buzzstate;
buzzstate = digitalRead(buzz);
     
    if (buzzstate == HIGH)
        {
            display.clearDisplay();
        display.setCursor(2, 2);
        display.print("ALARM");
        display.display();
        }
     
        else if (buzzstate == LOW)
        { 
            display.clearDisplay();
    display.setCursor(2, 2);
    display.print("SAFE");
    display.display();

    }
    else {}
}

Credits

Zachary Hunnicutt

Zachary Hunnicutt

1 project • 0 followers
Jackson Stone

Jackson Stone

1 project • 0 followers

Comments