Back to Parent

unsigned long SET_TIME = 10000;    //10 seconds
int pir = D0;                
int led = D1;  
int alarm = D7;
int pirState = LOW;      
int val = LOW; 
int detected = 0;
unsigned long onTime;
unsigned long currTime;
unsigned long timeDiff;                 
 
void setup() {
  pinMode(pir, INPUT); 
  pinMode(led, OUTPUT); 
  pinMode(alarm, OUTPUT);
  digitalWrite(led,LOW);
  Particle.variable("status", &detected, INT);
  Serial.begin(9600);
}

void loop(){
  tone(alarm, 1000); 
  delay(1000);        
  noTone(alarm);     
  delay(1000); 
  val = digitalRead(pir);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    if ((pirState ==  LOW) && (detected == 0)){
      digitalWrite(led,HIGH);
      detected = 1;
      delay(2000);
      Serial.print("detected\n");
    }
    else{
        digitalWrite(led,LOW);      //indicate motion still present
        delay(100);
        digitalWrite(led,HIGH);
        delay(100);
    }
    onTime = millis();    //set most recent detect time
    pirState = HIGH;
  }
  else {
    currTime = millis();
    timeDiff = currTime-onTime; 
    if (timeDiff >= SET_TIME){
      digitalWrite(led,LOW);
      detected = 0;
    }
    pirState = LOW;
  } 
}
Click to Expand

Content Rating

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

0