LowPowerLab Forum

Hardware support => Moteino => Topic started by: rocsta on September 01, 2015, 03:41:59 AM

Title: Strange Behaviour Interrupt / EEProm-Write
Post by: rocsta on September 01, 2015, 03:41:59 AM
Hi all,

back again and still working on my TrapControl-Project.

Now I have a strange behaviour, when using an interrupt and a function storing configuration values in the eeprom.
As you perhaps know, I use a pir sensor, using a Interrupt to close the trap. I initialize the Interrupt as follows:

    pinMode(IRQ_PIR, INPUT); // Sensor
    digitalWrite(IRQ_PIR, LOW); // Eingang auf LOW setzen
    attachInterrupt(IRQ_PIR, motionIRQ, RISING);

Everything works fine.
But: when I call my function to save the settings:

void save_settings() {
  for (unsigned int t = 0; t < sizeof(tc_configuration); t++)
    EEPROM.write(CONFIG_START + t, *((char*)&tc_configuration + t));
  }

... the Interrupt "fires".

Do you have any idea why?

Kind regards from Germany,

rocsta
Title: Re: Strange Behaviour Interrupt / EEProm-Write
Post by: Felix on September 01, 2015, 07:52:58 AM
Are you sure the interrupt fires?
Is it because the interrupt pin RISEs?
Title: Re: Strange Behaviour Interrupt / EEProm-Write
Post by: TomWS on September 01, 2015, 08:04:09 AM
What type of output does the PIR sensor have? In the code below you are configuring the pin as input with no pullup.  Do you have your own pulldown resistor on the pin or is it, perhaps, floating?

Quote from: rocsta on September 01, 2015, 03:41:59 AM

    pinMode(IRQ_PIR, INPUT); // Sensor
    digitalWrite(IRQ_PIR, LOW); // Eingang auf LOW setzen
    attachInterrupt(IRQ_PIR, motionIRQ, RISING);


Tom
Title: Re: Strange Behaviour Interrupt / EEProm-Write
Post by: rocsta on September 01, 2015, 08:34:26 AM
Quote from: Felix on September 01, 2015, 07:52:58 AM
Are you sure the interrupt fires?
Is it because the interrupt pin RISEs?

To be honest, I guess so.

This is my "motionIRQ":


void motionIRQ() {       
  // PIR Sensor "has something"
  motionDetected=true;
  Serial.println("Motion detected**************************************************");
  if (tc_configuration.trap_activated == 1)
     {...}
  }


I tried the following:
- I defined a key in my command Interface via serial  port "U" which just and only starts the function "save_settings" - nothing else
- Then I get on the Serial Interface: "Motion detected**************************************************"

And I also tried other of my keys, no "Motion detected" when I use keys, which don't call "save_setting()". When there is a "save_settings()"-Call, then I receive the output.

And finally, I reduced the content of "loop()" to "save_settings()". Result: "Motion detected".


So, I think the Interrupt "fires".

Title: Re: Strange Behaviour Interrupt / EEProm-Write
Post by: rocsta on September 01, 2015, 08:46:17 AM
Quote from: TomWS on September 01, 2015, 08:04:09 AM
What type of output does the PIR sensor have? In the code below you are configuring the pin as input with no pullup.  Do you have your own pulldown resistor on the pin or is it, perhaps, floating?

Stupid question: Is the output in my case important ? (I would like understand that)
I use this "HC-SR501 PIR MOTION DETECTOR", modifed to 3,3 V, as Felix described somewhere.

So next try:
I disconnected the output of the PIR-Sensor, I received:
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
...


Now, I'm totally confused.
I thought "RAISING" means, the Signal goes from "LOW" to "HIGH", but, when disconnected, there can't be a "HIGH" signal.
:-\
Title: Re: Strange Behaviour Interrupt / EEProm-Write
Post by: TomWS on September 01, 2015, 11:38:28 AM
Quote from: rocsta on September 01, 2015, 08:46:17 AM
Quote from: TomWS on September 01, 2015, 08:04:09 AM
What type of output does the PIR sensor have? In the code below you are configuring the pin as input with no pullup.  Do you have your own pulldown resistor on the pin or is it, perhaps, floating?

Stupid question: Is the output in my case important ? (I would like understand that)
Yes, it is.  If you want the input to be pulled up by the processor's internal pullup resistor then don't do the digitalWrite(), instead change pinMode to: 
pinMode(pin,INPUT_PULLUP);

If you want a pulldown (the PIR pulls the pin high on activation, then keep pinMode as you have it and add a resistor between the pin and ground.

Quote
I use this "HC-SR501 PIR MOTION DETECTOR", modifed to 3,3 V, as Felix described somewhere.

So next try:
I disconnected the output of the PIR-Sensor, I received:
Motion detected**************************************************
Motion detected**************************************************
Motion detected**************************************************
<snip>
...

Your pin is probably floating.
Quote
Now, I'm totally confused.
I thought "RAISING" means, the Signal goes from "LOW" to "HIGH", but, when disconnected, there can't be a "HIGH" signal.
:-\
It does, however, a floating pin will drift to mid value where it is susceptible to electrical noise.  I haven't used this sensor but a quick read of a description I found seems to indicate that the output is usually floating and will be pulled high when something is first detected and then will be pulled low when that something leaves.

I suggest installing a resistor to ground.  Assuming that the PIR is rarely high, then a 10K resistor should be ok. 

Tom