Back to Parent

//Mom (photon)

int ledPin;
int blink_delay;
int reps;
int alarm_pin = D6;
int buzzer_delay = 45;
int buzzer_times = 7;
int led_sense=D1;
int motion_pin=A0;
int motion_state = -1;
int motion_threshold = 2000;
bool motion_event = FALSE;
int count = 0;




//happens once
//what pins we want to act on
//read/write- pinMode(pin,mode)
void setup()
{
  pinMode( ledPin, OUTPUT);
  pinMode( led_sense, OUTPUT);
  pinMode( motion_pin, INPUT);
  pinMode(alarm_pin, OUTPUT);
  Particle.subscribe("bz/button", buzzer_alarm_cmd);
  /*Particle.function("bz/button_func",buttonToggle);*/

  Serial.begin(9600);

}

void loop()
{
  motion_state = analogRead(motion_pin);
  if (count < 11)
  {
    if (motion_state > motion_threshold)
      {
        digitalWrite(D0 , HIGH);
        Particle.publish("bz/motion_detect_on", "on");
        blinking(D1,100,10);

        if (motion_event == FALSE)
          /*buzzer_alarm(12);*/

        motion_event = TRUE;
        count = count+1;
      }
    else
      {
        digitalWrite(D0, LOW);
        if (count > 0)
          count = count-1;
        if (motion_event==TRUE)
          {
            Particle.publish("bz/motion_detect_off", "off");
            blinking(D1,200,5);
          }
        else{}
        motion_event = FALSE;
        delay(500);
      }
  }
  else
  {
    delay(30000);
    count = 0;
  }

  Serial.print("motion value ");
  Serial.println( motion_state );
  delay(250);


}

//Functions That Make Life Easier

int blinking(int ledPin, int blink_delay, int reps)
{
  pinMode( ledPin, OUTPUT);
  for (int i =1; i <= reps; i++)
  {
    digitalWrite( ledPin, HIGH);
    delay(blink_delay);
    digitalWrite( ledPin, LOW);
    delay(blink_delay);
  }

  return 1;
}

void buzzer_alarm(int buzzer_times)
{
  for (int i =1; i <= buzzer_times; i++)
  {
    digitalWrite( alarm_pin, HIGH);
    delay(buzzer_delay);
    digitalWrite( alarm_pin, LOW);
    delay(buzzer_delay);
  }





}

void buzzer_alarm_cmd(const char *event, const char *data)
{
  buzzer_alarm(30);
  delay(1000);

}

int buttonToggle(String command)
{

    if (command=="on") {
        buzzer_alarm(8);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(alarm_pin,LOW);
        return 0;
    }
    else {
        return -1;
    }

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0