I wanted to connect a PIR motion sensor to a photon and had weird erratic behavior. So I decided to totally removed the sensor, just the photon. Below the piece of code.
basically, attachInterrupt on D5, set few bool variables in the ISR, check these variables in the loop, increase stage variable value, then arm a timer to reset some variables.
The weird thing is when I pass my hand over the photon (without touching) it fires the interrupt… although it turns out to be a short range motion detection, I ma wondering whether this is a normal behavior. Am I missing something?
Thank you for your help.
regards.
#include "application.h"
#include "TimeAlarms.h"
const int Pin_MOTION = D5 ;
int pinValue = LOW;
int stage = 1;
bool motionStop = false;
bool motionRead = false;
int calibrated(int calibrateTime) {
return (millis() - calibrateTime);
}
void motionDetected() {
if (motionRead) {
pinValue = digitalRead(Pin_MOTION);
if (pinValue == HIGH) {
motionStop = false;
} else {
Serial.println("setMotionStop");
motionStop = true;
}
}
}
void setMotionRead() {
Serial.println("setMotionRead");
motionRead = true;
}
void setup() {
Serial.begin(9600);
pinMode(Pin_MOTION, INPUT);
digitalWrite(Pin_MOTION, LOW);
while (calibrated(10000)<0);
Serial.println("PIR calibrated");
motionRead = true;
attachInterrupt(Pin_MOTION, motionDetected, CHANGE);
}
void loop() {
Alarm.delay(3000);
if (motionStop && motionRead) {
motionRead = false;
motionStop = false;
Serial.print("Stage: "); Serial.println(stage);
switch (stage) {
case 1: stage++; break;
case 2: stage++; break;
case 3: stage++; break;
case 4: stage = 1; break;
}
Alarm.timerOnce(3, setMotionRead);
}
}