PIR with no interrupt - doubts.



  • Hi All,

    i'm trying to make my motion sensor work, without using interrupt.

    / Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable debug prints
    #define MY_BAUD_RATE 9600
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    #define MY_RF24_PA_LEVEL   RF24_PA_HIGH                                 // RF24_PA_MIN = -18dBm  RF24_PA_LOW = -12dBm RF24_PA_HIGH = -6dBm RF24_PA_MAX = 0dBm
    //#define MY_RF24_CHANNEL   (76)  // RF channel for the sensor net, 0-125.  //  0 => 2400 Mhz (RF24 channel 1)
                                                                                //  1 => 2401 Mhz (RF24 channel 2)
                                                                                // 76 => 2476 Mhz (RF24 channel 77)
                                                                                // 83 => 2483 Mhz (RF24 channel 84)
                                                                                // 124 => 2524 Mhz (RF24 channel 125)
                                                                                // 125 => 2525 Mhz (RF24 channel 126)
    //#define MY_RF24_BASE_RADIO_ID   0x00,0xFC,0xE1,0xA8,0xA8
    //#define MY_RF24_DATARATE   (RF24_250KBPS)                                 // RF24_250KBPS for 250kbs  RF24_1MBPS for 1Mbps  RF24_2MBPS for 2Mbps.
    //#define MY_RF24_SPI_SPEED   (2*1000000ul)                                 // Define this if you need to run the SPI clock at a different frequency than the default. Default nRF24L01+ SPI speed, 2MHz should be safe for nRF24L01+ clones.
    //#define MY_DEBUG_VERBOSE_RF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    
    // Node and sketch information
    #define SKETCH_VER            "1.0"				        // Sketch version
    #define SKETCH_NAME           "Motion Sensor NoINterrupt"		// Optional child sensor name
    #define CHILD_ID              1				           	// Id of the sensor child
    #define CHILD_NAME            "Motion Sensor NI"		// Optional child sensor name
    #define NODE_REPEATER         false		        	  	// Set true if a repeater node (i.e. always turned on) 
    
    int PIRpin   = 3;   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    int LEDpin   = 13;
    int PIRstate = LOW;             // we start, assuming no motion detected
    int value    = 0;                    // variable for reading the pin status
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    
    void setup()
    {
        pinMode(LEDpin, OUTPUT);      // declare LED as output
        pinMode(PIRpin, INPUT);     // declare sensor as input
    }
    
    void presentation()
    {
      sendSketchInfo(SKETCH_NAME, SKETCH_VER);  
      present(CHILD_ID, S_MOTION);
    }
    
    void loop(){
      value = digitalRead(PIRpin);   // read input value
      if (value == HIGH) {           // check if the input is HIGH
        digitalWrite(LEDpin, HIGH);  // turn LED ON
        if (PIRstate == LOW) {
          // we have just turned on
          Serial.println("Motion detected!");
          send(msg.set(PIRstate==LOW ? 1 : 0));
          // We only want to print on the output change, not state
          PIRstate = HIGH;
        }
      } else {
        digitalWrite(LEDpin, LOW); // turn LED OFF
        if (PIRstate == HIGH){
          // we have just turned of
          Serial.println("Motion ended!");
          send(msg.set(PIRstate==HIGH ? 1 : 0));
          // We only want to print on the output change, not state
          PIRstate = LOW;
        }
      }
    }
    

    but what worked before (without mysensor) is not working anymore. and the Serial.println shows nothing.

    Any hint in how to debug with the Serial.println?

    It compiles with no error, but does not work.

    Thx in advance



  • If it stopped working after adding mysensors then it must be at fault.
    Do you see normal mysensors debug messages?
    Does it connect to gateway?
    Try adding :

    #define MY_TRANSPORT_WAIT_READY_MS 5000
    
    

    before

    #include <MySensors.h>
    

    that will allow to execute your loop(); even without radio link working.



  • @Sasquatch

    got it working like, but there is problem: every time it's triggered, motion detected - motion ended and a second time motion detected - motion ended. not finding out why it's triggered a second time.

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable debug prints
    //#define MY_BAUD_RATE 9600
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    #define MY_RF24_PA_LEVEL   RF24_PA_HIGH                                 // RF24_PA_MIN = -18dBm  RF24_PA_LOW = -12dBm RF24_PA_HIGH = -6dBm RF24_PA_MAX = 0dBm
    //#define MY_RF24_CHANNEL   (76)  // RF channel for the sensor net, 0-125.  //  0 => 2400 Mhz (RF24 channel 1)
                                                                                //  1 => 2401 Mhz (RF24 channel 2)
                                                                                // 76 => 2476 Mhz (RF24 channel 77)
                                                                                // 83 => 2483 Mhz (RF24 channel 84)
                                                                                // 124 => 2524 Mhz (RF24 channel 125)
                                                                                // 125 => 2525 Mhz (RF24 channel 126)
    //#define MY_RF24_BASE_RADIO_ID   0x00,0xFC,0xE1,0xA8,0xA8
    //#define MY_RF24_DATARATE   (RF24_250KBPS)                                 // RF24_250KBPS for 250kbs  RF24_1MBPS for 1Mbps  RF24_2MBPS for 2Mbps.
    //#define MY_RF24_SPI_SPEED   (2*1000000ul)                                 // Define this if you need to run the SPI clock at a different frequency than the default. Default nRF24L01+ SPI speed, 2MHz should be safe for nRF24L01+ clones.
    //#define MY_DEBUG_VERBOSE_RF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    
    // Node and sketch information
    #define SKETCH_VER            "1.0"				                  // Sketch version
    #define SKETCH_NAME           "Motion Sensor NoINterrupt"		// Optional child sensor name
    #define CHILD_ID              1				                    	// Id of the sensor child
    #define CHILD_NAME            "Motion Sensor NI"	        	// Optional child sensor name
    #define NODE_REPEATER         false		        	          	// Set true if a repeater node (i.e. always turned on) 
    
    int PIRpin   = 3;      // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    int LEDpin   = 13;
    int PIRstate = LOW;    // we start, assuming no motion detected
    int value    = 0;      // variable for reading the pin status
    int PIRsold  = 0;
    
    // Initialize motion message
    MyMessage msgPIR(CHILD_ID, V_TRIPPED);
    
    
    void setup()
    {
        pinMode(LEDpin, OUTPUT);    // declare LED as output
        pinMode(PIRpin, INPUT);     // declare sensor as input
    }
    
    void presentation()
    {
      sendSketchInfo(SKETCH_NAME, SKETCH_VER);  
      present(CHILD_ID, S_MOTION);
    }
    
    void loop(){
    
      value = digitalRead(PIRpin);   // read input value
      if (value == HIGH) {           // check if the input is HIGH
        digitalWrite(LEDpin, HIGH);  // turn LED ON
        if (PIRstate == LOW) {
          Serial.println("Motion detected!");
          send(msgPIR.set(1));
          PIRstate = HIGH;
        }
      } else {
        digitalWrite(LEDpin, LOW);   // turn LED OFF
        if (PIRstate == HIGH){
          Serial.println("Motion ended!");
          send(msgPIR.set(0));
          PIRstate = LOW;
        }
      }
    }
    

    Thx for the help.


  • Mod

    @pw44 Why is your loop so complex? You only need to detect changes in PIRpin value...

    int prevValue = LOW
    void loop()
    {
      int value = digitalRead(PIRpin);   // read input value
      digitalWrite(LEDpin, value);       // LED mirrors PIRpin value
      if (value != prevValue)
      {
          Serial.println(value ? F("Motion detected!") : F("Motion ended!"));
          send(msgPIR.set(value));       // send PIRpin value
          prevValue = value;             // remember last value, to be able to detect changes
      }
    }
    

    If you still see double triggers with this code this will be because there are indeed double triggers coming from your PIR...



  • @Yveaux Thx for the hint! BR


Log in to reply
 

Suggested Topics

  • 2
  • 4
  • 11
  • 6
  • 9
  • 14
  • 7
  • 9

239
Online

9.8k
Users

10.3k
Topics

106.7k
Posts