First project on a photon and I’m having a minor issue with my motion sensor interrupt. For some reason “motion detected” gets published twice every time I trigger the motion sensor, no matter how long a delay I put in. For example right now I have a delay of 5 seconds in the code. Triggering this once will always result in the light staying on for 10 seconds and two “Motion Detected” lines being published to the dashboard. Any idea how to fix this?
The motion sensor is here: https://www.sparkfun.com/products/13285
Code:
// -----------------------------------------
// Publish Motion Detection
// -----------------------------------------
// Pin Assignments
int boardLed = D7; // On board LED
int motionSensor = A0; // Motion sensor signal pin
// Variable Assignments
bool motionDetect=false;
void setup() {
pinMode(boardLed,OUTPUT); // on-board LED is output
pinMode(motionSensor, INPUT);
attachInterrupt(motionSensor, ISR, FALLING);
}
void loop() {
if(motionDetect==true) {
noInterrupts();
motionDetect=false;
Spark.publish("Motion Detected");
digitalWrite(boardLed,HIGH);
delay(5000);
digitalWrite(boardLed,LOW);
interrupts();
}
}
void ISR(void) {
motionDetect=true;
}