Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. PIR with no interrupt - doubts.

PIR with no interrupt - doubts.

Scheduled Pinned Locked Moved Troubleshooting
5 Posts 3 Posters 79 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • pw44P Offline
    pw44P Offline
    pw44
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sasquatch
      wrote on last edited by Sasquatch
      #2

      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.

      pw44P 1 Reply Last reply
      0
      • S Sasquatch

        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.

        pw44P Offline
        pw44P Offline
        pw44
        wrote on last edited by pw44
        #3

        @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.

        YveauxY 1 Reply Last reply
        0
        • pw44P pw44

          @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.

          YveauxY Offline
          YveauxY Offline
          Yveaux
          Mod
          wrote on last edited by
          #4

          @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...

          http://yveaux.blogspot.nl

          pw44P 1 Reply Last reply
          1
          • YveauxY Yveaux

            @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...

            pw44P Offline
            pw44P Offline
            pw44
            wrote on last edited by
            #5

            @Yveaux Thx for the hint! BR

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            19

            Online

            11.7k

            Users

            11.2k

            Topics

            113.1k

            Posts


            Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • MySensors
            • OpenHardware.io
            • Categories
            • Recent
            • Tags
            • Popular