Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Javier LeonBryan Doria Gomez
Published

Motion Alarm 9000

The Motion Alarm 9000 will alert you when anyone enters your room.

IntermediateFull instructions provided1 hour1,130
Motion Alarm 9000

Things used in this project

Story

Read more

Schematics

Motion alarm schematic

Code

Motion Sensor

Arduino
// First photon, Motion sensor
int inputPin = D0;              
int ledPin = D1;                
int pirState = LOW;             
int val = 0;
int light = D7;

int calibrateTime = 10000;      

void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     
  digitalWrite(light, LOW);
  pinMode(light, OUTPUT);
  Particle.subscribe("bothways", event2, "400021000c47363433353735");
}
void setLED( int state )
{
  digitalWrite( ledPin, state );
}
void event2(const char *event, const char *data)
{
  digitalWrite(light, HIGH);
  delay(500);
  digitalWrite(light, LOW);
  //Particle.publish("test", "on");
}
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("Motion1", "Detected");
    //  delay(1500);
      
      pirState = HIGH;
      setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
     
      pirState = LOW;
      setLED( pirState );
    }
  }
}

Led alarm

Arduino
//Second Photon, LED
int led = D7;
int led2 = D4;
int led3 = D3;

void setup() {

pinMode(led, OUTPUT);
digitalWrite(led, LOW);
pinMode(led2, OUTPUT);
digitalWrite(led2, LOW);
pinMode(led3, OUTPUT);
digitalWrite(led3, LOW);
Particle.subscribe("Motion1", motion_detected, "34001b000947363339343638");


}

void motion_detected(const char *event, const char *data)
{

  Particle.publish("bothways", "on");
 // digitalWrite(led, HIGH);
  	unsigned long startTime = millis();
	while (digitalRead(D6) != LOW && millis() - startTime < 10000) {
        digitalWrite(led2, HIGH);
        digitalWrite(led, HIGH);
        delay(100);
        digitalWrite(led2, LOW);
        digitalWrite(led, LOW);
        delay(100);
        digitalWrite(led3, HIGH);
        digitalWrite(led, HIGH);
        delay(100);
        digitalWrite(led3, LOW);
        digitalWrite(led, LOW);
	}
//    digitalWrite(led, LOW);
}

Credits

Javier Leon
1 project • 0 followers
Contact
Bryan Doria Gomez
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.