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. Help troubleshooting code

Help troubleshooting code

Scheduled Pinned Locked Moved Troubleshooting
13 Posts 3 Posters 2.4k 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.
  • Cliff KarlssonC Offline
    Cliff KarlssonC Offline
    Cliff Karlsson
    wrote on last edited by
    #1

    I want to use a PIR to enable two ultrasonic sensors for 30 seconds like this:

    1 PIR detects movement
    2 PIR is disabled for 30 seconds
    3 Both ultrasonic sensors is enbabled for 30 seconds
    4 Ultrasonic sensors OFF PIR ON
    5 --> Step one

    But I cant get my code to complie.

    // Enable debug prints
    #define MY_DEBUG
    #define MY_BAUD_RATE 115200
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensor.h>  
    #include <NewPing.h>
    
    #define SONAR_A_CHILD_ID 1
    #define SONAR_B_CHILD_ID 2
    #define MOTION_A_CHILD_ID 3
    
    
    #define SONAR_A_TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define SONAR_A_ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MOTION_A_TRIGGER_PIN  2
    
    #define SONAR_B_TRIGGER_PIN  8  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define SONAR_B_ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
    
    #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    unsigned long SLEEP_TIME = 200; // Sleep time between reads (in milliseconds)
    
    unsigned long MOTION_SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    
    NewPing sonar_A(SONAR_A_TRIGGER_PIN, SONAR_A_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    NewPing sonar_B(SONAR_B_TRIGGER_PIN, SONAR_B_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    
    
    MyMessage SONAR_A_msg(SONAR_A_CHILD_ID, V_DISTANCE);
    int SONAR_A_lastDist;
    
    MyMessage SONAR_B_msg(SONAR_B_CHILD_ID, V_DISTANCE);
    int SONAR_B_lastDist;
    
    MyMessage MOTION_A_msg(MOTION_A_CHILD_ID, V_TRIPPED);
    
    boolean metric = true; 
    
    void setup()  
    { 
      metric = getConfig().isMetric;
      pinMode(MOTION_A_TRIGGER_PIN, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Distance+Motion Sensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(SONAR_A_CHILD_ID, S_DISTANCE);
      present(SONAR_B_CHILD_ID, S_DISTANCE);
      present(MOTION_A_TRIGGER_PIN, S_MOTION);
    }
    
    void loop()      
    {     
      
      boolean tripped = digitalRead(MOTION_A_TRIGGER_PIN) == HIGH; 
      
        
      Serial.println(tripped);
      send(MOTION_A_msg.set(tripped?"1":"0"));  // Send tripped value to gw 
     if(tripped)  {
      
      int SONAR_A_dist = metric?sonar_A.ping_cm():sonar_A.ping_in();
      Serial.print("Ping Sonar A: ");
      Serial.print(SONAR_A_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println(metric?" cm":" in");
    
      if (SONAR_A_dist != SONAR_A_lastDist) {
          send(SONAR_A_msg.set(SONAR_A_dist));
          SONAR_A_lastDist = SONAR_A_dist;
      }
      
      wait(1000);
    
      int SONAR_B_dist = metric?sonar_B.ping_cm():sonar_B.ping_in();
      Serial.print("Ping Sonar B: ");
      Serial.print(SONAR_B_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println(metric?" cm":" in");
    
      if (SONAR_B_dist != SONAR_B_lastDist) {
          send(SONAR_B_msg.set(SONAR_B_dist));
          SONAR_B_lastDist = SONAR_B_dist;
      }
    
      sleep(SLEEP_TIME);
    }
                }
    //else{
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME);
    //}
    }
    reply
    
    1 Reply Last reply
    0
    • barduinoB Offline
      barduinoB Offline
      barduino
      wrote on last edited by
      #2

      Hi @Cliff-Karlsson

      In this line

      #define INTERRUPT DIGITAL_INPUT_SENSOR-2
      

      DIGITAL_INPUT_SENSOR does not exist, Im assuming its the pin from the PIR so replace it with

      #define INTERRUPT MOTION_A_CHILD_ID-2
      

      Also you have an extra } in your code

      if (SONAR_B_dist != SONAR_B_lastDist) {
            send(SONAR_B_msg.set(SONAR_B_dist));
            SONAR_B_lastDist = SONAR_B_dist;
        }
      
        sleep(SLEEP_TIME);
      }
                  }
      //else{
        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
        sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME);
      //}
      }
      

      Replace with

      if (SONAR_B_dist != SONAR_B_lastDist) {
            send(SONAR_B_msg.set(SONAR_B_dist));
            SONAR_B_lastDist = SONAR_B_dist;
          }
      
          sleep(SLEEP_TIME);
      
        }
        //else{
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
        sleep(INTERRUPT, CHANGE, MOTION_SLEEP_TIME);
        //}
      }
      

      Cheers

      martinhjelmareM 1 Reply Last reply
      0
      • barduinoB barduino

        Hi @Cliff-Karlsson

        In this line

        #define INTERRUPT DIGITAL_INPUT_SENSOR-2
        

        DIGITAL_INPUT_SENSOR does not exist, Im assuming its the pin from the PIR so replace it with

        #define INTERRUPT MOTION_A_CHILD_ID-2
        

        Also you have an extra } in your code

        if (SONAR_B_dist != SONAR_B_lastDist) {
              send(SONAR_B_msg.set(SONAR_B_dist));
              SONAR_B_lastDist = SONAR_B_dist;
          }
        
          sleep(SLEEP_TIME);
        }
                    }
        //else{
          // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
          sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME);
        //}
        }
        

        Replace with

        if (SONAR_B_dist != SONAR_B_lastDist) {
              send(SONAR_B_msg.set(SONAR_B_dist));
              SONAR_B_lastDist = SONAR_B_dist;
            }
        
            sleep(SLEEP_TIME);
        
          }
          //else{
          // Sleep until interrupt comes in on motion sensor. Send update every two minute.
          sleep(INTERRUPT, CHANGE, MOTION_SLEEP_TIME);
          //}
        }
        

        Cheers

        martinhjelmareM Offline
        martinhjelmareM Offline
        martinhjelmare
        Plugin Developer
        wrote on last edited by
        #3

        @barduino

        Don't confuse MOTION_A_TRIGGER_PIN with MOTION_A_CHILD_ID.

        barduinoB 1 Reply Last reply
        0
        • martinhjelmareM martinhjelmare

          @barduino

          Don't confuse MOTION_A_TRIGGER_PIN with MOTION_A_CHILD_ID.

          barduinoB Offline
          barduinoB Offline
          barduino
          wrote on last edited by
          #4

          @martinhjelmare

          You're right it should be MOTION_A_TRIGGER_PIN. And it should be -1 not -2 confirm?

          martinhjelmareM 1 Reply Last reply
          0
          • barduinoB barduino

            @martinhjelmare

            You're right it should be MOTION_A_TRIGGER_PIN. And it should be -1 not -2 confirm?

            martinhjelmareM Offline
            martinhjelmareM Offline
            martinhjelmare
            Plugin Developer
            wrote on last edited by martinhjelmare
            #5

            @barduino

            The interrupt number is usually pin - 2 on our boards. But I would rather use the function provided by arduino to get the correct interrupt number for a pin, digitalPinToInterrupt.

            1 Reply Last reply
            1
            • Cliff KarlssonC Offline
              Cliff KarlssonC Offline
              Cliff Karlsson
              wrote on last edited by
              #6

              Thanks for the help, the sketch complies and runs now. But where and how do I specify the timing in the sketch. This is what I want.

              1 PIR detects movement
              2 PIR is disabled for 30 seconds
              3 Both ultrasonic sensors is enbabled for 30 seconds
              4 Ultrasonic sensors OFF PIR ON
              5 --> Step one

              1 Reply Last reply
              0
              • Cliff KarlssonC Offline
                Cliff KarlssonC Offline
                Cliff Karlsson
                wrote on last edited by
                #7

                I don't really understand how the sketch that I posted works. After motionsensor detects movement the loop under

                if (tripped)
                

                runs, but for how long are the ultrasonic sensors active? And how do I make it so that they are active for 30 sec before they are disabled and the PIR starts again.

                martinhjelmareM 1 Reply Last reply
                0
                • Cliff KarlssonC Cliff Karlsson

                  I don't really understand how the sketch that I posted works. After motionsensor detects movement the loop under

                  if (tripped)
                  

                  runs, but for how long are the ultrasonic sensors active? And how do I make it so that they are active for 30 sec before they are disabled and the PIR starts again.

                  martinhjelmareM Offline
                  martinhjelmareM Offline
                  martinhjelmare
                  Plugin Developer
                  wrote on last edited by
                  #8

                  @Cliff-Karlsson

                  Should the node sleep in between pinging during the 30 secs that the sonars should be active?

                  1 Reply Last reply
                  0
                  • Cliff KarlssonC Offline
                    Cliff KarlssonC Offline
                    Cliff Karlsson
                    wrote on last edited by
                    #9

                    @Cliff-Karlsson said:

                    I don't really understand how the sketch that I posted works. After motionsensor detects movement the loop under

                    No I only want the sensor to sleep until it gets interrupted from the PIR. Then I want the ultrasonic sensors to ping continuously during the 30 secs.

                    1 Reply Last reply
                    0
                    • martinhjelmareM Offline
                      martinhjelmareM Offline
                      martinhjelmare
                      Plugin Developer
                      wrote on last edited by
                      #10

                      Right now there's a call to wait method in between A and B pings, waiting 1 second. Is that intended? For 30 secs, ping A and B alternatively every 1 second?

                      Cliff KarlssonC 1 Reply Last reply
                      0
                      • martinhjelmareM martinhjelmare

                        Right now there's a call to wait method in between A and B pings, waiting 1 second. Is that intended? For 30 secs, ping A and B alternatively every 1 second?

                        Cliff KarlssonC Offline
                        Cliff KarlssonC Offline
                        Cliff Karlsson
                        wrote on last edited by
                        #11

                        @martinhjelmare

                        Well I thing the sketch was created mostly using copy/paste so that bit is not exactly what I wanted.

                        For 30secs, ping A and B alternatively every 100ms would probably be better.

                        1 Reply Last reply
                        0
                        • Cliff KarlssonC Offline
                          Cliff KarlssonC Offline
                          Cliff Karlsson
                          wrote on last edited by
                          #12

                          I don't understand where and how I should specify that the ultrasonic part should run for 30 sec.

                          void loop()      
                          {     
                            
                            boolean tripped = digitalRead(MOTION_A_TRIGGER_PIN) == HIGH; 
                            
                              
                            Serial.println(tripped);
                            send(MOTION_A_msg.set(tripped?"1":"0"));  // Send tripped value to gw 
                           if(tripped)  {
                            
                            int SONAR_A_dist = metric?sonar_A.ping_cm():sonar_A.ping_in();
                            Serial.print("Ping Sonar A: ");
                            Serial.print(SONAR_A_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
                            Serial.println(metric?" cm":" in");
                          
                            if (SONAR_A_dist != SONAR_A_lastDist) {
                                send(SONAR_A_msg.set(SONAR_A_dist));
                                SONAR_A_lastDist = SONAR_A_dist;
                            }
                            
                            wait(1000);
                          
                            int SONAR_B_dist = metric?sonar_B.ping_cm():sonar_B.ping_in();
                            Serial.print("Ping Sonar B: ");
                            Serial.print(SONAR_B_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
                            Serial.println(metric?" cm":" in");
                          
                            if (SONAR_B_dist != SONAR_B_lastDist) {
                                send(SONAR_B_msg.set(SONAR_B_dist));
                                SONAR_B_lastDist = SONAR_B_dist;
                            }
                          
                            sleep(SLEEP_TIME);
                          }
                                      }
                          //else{
                            // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
                            sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME);```
                          1 Reply Last reply
                          0
                          • martinhjelmareM Offline
                            martinhjelmareM Offline
                            martinhjelmare
                            Plugin Developer
                            wrote on last edited by
                            #13

                            You have to add a timer, and an if statement. Start the timer after motion is tripped. If time is less than 30 s, ping. Else reset timer and sleep with motion interrupt.

                            I can post some example code later perhaps.

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


                            12

                            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