Shawn DashtySaul Avalos
Published © GPL3+

MEGR 3171 Fall 2018 - Security Motion Sensor

Our device serves a multipurpose motion sensor that will allow you to remain confident in your belongings whereabouts

BeginnerFull instructions provided5 hours1,167
MEGR 3171 Fall 2018 - Security Motion Sensor

Things used in this project

Story

Read more

Schematics

PIR Motion Sensor Photon Circuit Diagram

LED Photon Circuit Diagram

Positive end of LED goes into D7 pin location, while the negative goes to the GRND position adjacent to D7.

Code

Motion Sensor Photon Code

C/C++
This code is responsible for the main subscribing and publishing of the two photons. It has a calibration time of 10 seconds (1000 ms) in order for you to set the sensor and leave the room/location that you are keeping watch of. Once calibrated, it reads the sensor which can be found in the loop. After the sensor is read, the data is reported. This allows for the instantaneous data tracking that the project requires. Using a binary system, we used two variables: HIGH & LOW, which are equivalent to 1 & 0, respectively. If the value is retrieved as a HIGH/1, the output is given a value/amplitude of 1. This data point is in the form of a bar graph, which depicts the number of counts/trips within the minute of sensing motion. If the output is LOW/0, the code loops back to beginning, where calibration takes place and the entire process thereafter.
// First photon, Motion sensor
int inputPin = D0;              
int ledPin = D7;                
int pirState = LOW;             
int val = 0;                    

int calibrateTime = 10000;      

void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     
}

void loop()
{

  
  if ( calibrated() )
  {

    readTheSensor();

   
    reportTheData();
  }
}

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

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

void reportTheData() {

  
  if (val == HIGH) {

   
    if (pirState == LOW) {
     
      Particle.publish("robot_penguin", "1");
      
      pirState = HIGH;
      setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
     
      pirState = LOW;
      setLED( pirState );
    }
  }
}


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

LED/Buzzer Photon Code

C/C++
This code is used to assure the user of proper photon communication by the use of an LED light and piezo buzzer. When the first photon with the motion sensor is tripped, it calls to the subscribed photon with the LED to shine as response. In sequence, the buzzer goes off as well. This is what is what is seen in the loop. Both pin D7 and the LED should light up when the motion sensor is tripped. The code also allows for the led to pulsate 2times, with a delay of 100 ms in between each pulse. This is also true for the buzzer alarm.
int led = D7;
int Buzzer = D0;
int state = 0;



void setup() {

pinMode(led, OUTPUT);
digitalWrite(led, LOW);

pinMode(Buzzer,OUTPUT);
digitalWrite(Buzzer, LOW);
}




void myHandler(const char *event, const char *data)
{
  digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(100);
  
  digitalWrite(Buzzer,HIGH);
      delay(100);
  digitalWrite(Buzzer,LOW);
  
  digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(100);
  
  digitalWrite(Buzzer,HIGH);
      delay(100);
      digitalWrite(Buzzer,LOW);
      
  
  
}


void loop() {
Particle.subscribe("robot_penguin", myHandler, "2e0033000d47363330353437");
delay(1000);
}








    

Credits

Shawn Dashty

Shawn Dashty

1 project • 0 followers
Saul Avalos

Saul Avalos

1 project • 0 followers
Thanks to David Daniel, Kyle Byrd.

Comments