interrupt mystery with sensebender micro



  • Hi,

    I don't get interrupts working for door and PIR sensors. Any help appreciated.

    I connected door trigger on pin 3 (I assume thats D3) and PIR motion sensor on pin4 (D4). They are defined like this in code:
    https://github.com/ikke-t/sensebender/blob/master/SenseBender/SenseBender.ino#L114

    #define DOOR_PIN       3
    #define MOTION_PIN     4
    

    Then I try do stuff based on their value, waking up interrupt as described here:
    https://www.mysensors.org/download/sensor_api_20#sleeping

      sleep(digitalPinToInterrupt(DOOR_PIN), CHANGE,
            digitalPinToInterrupt(MOTION_PIN), CHANGE,
            MEASURE_INTERVAL);
    

    Somehow I don't get any changes from the PIR, and The door sensor only sends when the door opens. But then after couple times printing out values the serial from SB micro just goes bananas, outputting junk instead of readable stuff. Do the use of wakeup interrupt screw up the serial output?

    Another mystery is, that I read from some sample codes that only pins 2&3 can cause interrupts. But I don't see it mentioned in SB Micro page. And there is no D2 in SB micro, I assume it's used by the internal sensors. Is this true with SB Micro?

    So any idea how to make me understand how wrong I am? 🙂

    The code and all the necessary libraries are here:
    https://github.com/ikke-t/sensebender/blob/master/SenseBender/SenseBender.ino

    BTW, the door sensor worked fine until I changed it to be interruptible. But I don't want to poll in loop.

    Thanks,
    ikke


  • Admin

    D2 is connected to the radios IRQ pin on the sensebender.

    https://www.openhardware.io/dl/568d7feb0779a3581858ac2a/design/MySensorMicro.pdf

    So your sleep -ing will be interrupted by the radio IRQ pin.

    You could tap in to the D2 somewhere (and cut the leg off the radio module) to solve your dual external pin wake up problem.



  • Is it safe to cut off radio irq pin? So it's not used by the MySensors code?


  • Admin

    No, it's not used right now.


  • Admin

    D2 interrupt is not used by nrf24, but if you're going to use it with rfm69 (with an adapter board) then you need interrupt for the radio.



  • According to the above pic, there is plenty of interrupt pins. If thats what the int means in their name. Why can't I use other than the D2 for interrupt, and make sure D2 is not used for interrupts?


  • Admin

    D3 is also an interrupt. The rest of the pins also have a pin change interrupt function (haven't used it yet myself)



  • I used pins 3 and 4 for the interruptible input from sensors. What does D2 have to do with this in my case, if none of the code is using it for interrupts?

    I brought d2 and d3 up because some sample codes for sensors mentioned that only those can be used for interrupts. But I assume now this is not the case for SB, thanks to the pic above.


  • Admin

    The CHANGE interrupt can only be used on pin 2,3 of the Atmeta328

    https://www.arduino.cc/en/Reference/AttachInterrupt

    There are other ways of trigger wakeup on all pins, but they are not supported by the MySensors sleep function and requires out to keep track of which pin that was triggered manually.


  • Hardware Contributor

    +1 on what said @Hek 😉
    there is Pin change Int for other pins but you'll need to use raw registers stuff. There exists a lib for this but not supported by MYS sleep. And it's more efficient for memory consumption to use only what you need 🙂
    I will publish soon my multisensor sketch where I use D2/int0,D3/int1 and two others pinchange int but in case you will find everything you need for this here : : http://www.gammon.com.au/forum/?id=11497



  • @hek thanks, this clears the topic. I'll try cutting off the D2 and reuse that. I'll report back...



  • Good news and bad news... I got change based interrupts to work by moving the motion to D2. That required cutting off the radio IRQ. Naturally I lost one radio there cutting too much....

    So now it works initially. But after some rounds the serial text gets garbled, and it stops sending anything quite soon then.

    isMetric: 1
    TempDiff :127.31
    HumDiff  :151.00
    T: 27.31
    H: 51
    TempDiff :0.01
    HumDiff  :0.00
    Door  :0
    Motion:1
    TempDiff :0.01
    HumDiff  :0.00
    Door  :0
    Motion:0
    TempDiff :0.07
    HumDiff  :0.00
    Door  :0
    Motion:1
    TempDiff :0.10
    HumDiff  :0.00
    Door  :0
    Motion:0
    x00�����x00�����x00�x00x00��x00��x00x00�x00x00�x00x00x00�x00x00x00x00�x00�x00x00x00x00��x00x00�x00x00x00���������```
    

    the last line contines for long. Any ideas what could cause it? Some trick I missed with interrupts?

    Again, the fixed code is here, it's almost the original SB Micro sample:
    https://github.com/ikke-t/sensebender/blob/master/SenseBender/SenseBender.ino



  • Solved the scrambled test. I removed the interrupts, and made it loop faster. It always got screwed after five rounds, so it seemed to be due the clock speed change in the template:

    #ifndef MY_OTA_FIRMWARE_FEATURE
      if ((measureCount == 5) && highfreq) 
      {
        clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power.
        highfreq = false;
      } 
    #endif
    

    Removing that takes it further. I suppose the sample code is missing something to fix the serial speed after prescaler change. I also wonder if the radio would require some reconfig after clock speed change?



  • But weird still. If I set the sleep time to 10 secs, without interrupts it works just fine. If interrupts are enabled, it stops always in less than ten rounds. So could it be the interrupts break the radio part somehow? E.g. if interrupt comes during transmit? Are interrupts safe in arduino, or should there be some lock set or disable interrupts for time of radio transmit?


  • Mod

    @ikkeT Have a look at the other pin interrupt related post: https://forum.mysensors.org/topic/4999/solved-latest-git-snapshot-causes-freezes/38
    I fixed an issue that might also bother you.



  • @Yveaux Thanks, I was following the thread. I confirm, the change fixed my problem too, excellent.



  • @ikkeT, I've used the PIN 3 with a door sensor, it work perfectly, just enable the mode INPUT_PULLUP on the setup.

    void setup() {
    pinMode(DOOR_PIN, INPUT_PULLUP);


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 5
  • 1
  • 2
  • 6

49
Online

11.4k
Users

11.1k
Topics

112.6k
Posts