attachInterrupt and Wifi weird behavior?

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);
  }

}

@sharly09, when you select a CHANGE interrupt, no internal pull-up or pull-down is selected so the input will “float”. In this case the signal at the input pin must be low or high OR you can use a pull-up or pull-down resistor to prevent it from floating. In your case, you body holds a charge which will be picked up by the Photon, enough to change the pin state and trigger the interrupt! :smiley:

1 Like

@peekay123 That was fast! thank you for the reply. Too bad I thought I was on the “right side of the force” :smile:

@sharly09, if you are using Particle devices you ARE on the right side of the force! :stuck_out_tongue:

You know, that probably explains some proximity capacitance problems I had a while back in another (non-Particle) project. I was using an ISR to handle a pushbutton, and I was getting spurious button clicks whenever I passed my hand near the circuit. And when I read this, I remembered that I had changed it at some point to use a CHANGE state instead of FALLING, as I had originally had it.

Now that I know, when I go back to that project, maybe I won’t drive myself nuts anymore. :smile: