Help with sketch (motion/distance)



  • Can anyone tell me how to fix this sketch so that it woorks correctly.

    I want the sensor to wake up when motion-sensor is triggered and start sending distance data from both Ultrasonic-sensors to controller for like 5s. Then back to sleep again until Motion is detected again.

    
    // 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 <NewPing.h>
    #include <MySensors.h>
    #include <SPI.h>
    
    #define SONAR_A_CHILD_ID 1
    #define SONAR_B_CHILD_ID 2
    #define MOTION_A_CHILD_ID 3
    
    #define MOTION_A_TRIGGER_PIN  3
    
    #define SONAR_A_TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define SONAR_A_ECHO_PIN     4 // Arduino pin tied to echo pin on the ultrasonic sensor.
    
    
    #define SONAR_B_TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define SONAR_B_ECHO_PIN     6  // 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 MOTION_A_TRIGGER_PIN-3 // 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 = getControllerConfig().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)  {
      unsigned long time = millis()+30000;
      while (millis() < time){
          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(100);
    
          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);
    //}
    }
    

  • Mod

    @Cliff-Karlsson the sketch looks good to me, but it is a bit hard to read since indentation is kind of random. (Use ctrl+t in Arduino IDE to make it easier to read).

    One thing though,

    time = millis()+30000
    

    should probably be

    time = millis()+5000
    

    if you only want to send distance for 5 seconds.

    What happens when you use the node now? What in the behavior is different from what you want?



  • Thanks, well firstly the motion-sensor almost never triggers and when it finaly does I get only 0 or a few (1-2) readings from the Ultrasonic-sensor and it seams that only one of them gives me distance.


  • Mod

    There are good serial debug prints in the sketch. Could you post the debug output, along with a description of when you triggered the motion detection?


  • Mod

    Here is the formatted sketch in case anyone else is unable to read the original

    
    // 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 <NewPing.h>
    #include <MySensors.h>
    #include <SPI.h>
    
    #define SONAR_A_CHILD_ID 1
    #define SONAR_B_CHILD_ID 2
    #define MOTION_A_CHILD_ID 3
    
    #define MOTION_A_TRIGGER_PIN  3
    
    #define SONAR_A_TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define SONAR_A_ECHO_PIN     4 // Arduino pin tied to echo pin on the ultrasonic sensor.
    
    
    #define SONAR_B_TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define SONAR_B_ECHO_PIN     6  // 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 MOTION_A_TRIGGER_PIN-3 // 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 = getControllerConfig().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)  {
        unsigned long time = millis() + 30000;
        while (millis() < time) {
          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(100);
    
          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);
      //}
    }
    


  • Re: Help with sketch (motion/distance)

    I had some additional wiring problems so the distance sensors seams to work ok now. But It does not look like the motion-sensor part works.

    I was thinking that the sensor should sleep until motion but it should report the status to the controller every 120s so that domoticz etc. does not think that the sensor is dead.

    Now it starts the distance sensors directly on power and runs them for a long time and then sleeps. But I cant wake it up by moving my hand in front of it. I think it wakes up every 120s and spams the distance sensor then sleeps agin.

    0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
    3 TSM:INIT
    4 TSF:WUR:MS=0
    11 TSM:INIT:TSP OK
    13 TSF:SID:OK,ID=2
    14 TSM:FPAR
    51 TSF:MSG:SEND,2-2-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2058 !TSM:FPAR:NO REPLY
    2060 TSM:FPAR
    2096 TSF:MSG:SEND,2-2-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2934 TSF:MSG:READ,0-0-2,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    2939 TSF:MSG:FPAR OK,ID=0,D=1
    4104 TSM:FPAR:OK
    4105 TSM:ID
    4106 TSM:ID:OK
    4108 TSM:UPL
    4113 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    4219 TSF:MSG:READ,0-0-2,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    4224 TSF:MSG:PONG RECV,HP=1
    4227 TSM:UPL:OK
    4228 TSM:READY:ID=2,PAR=0,DIS=1
    4233 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    4366 TSF:MSG:READ,0-0-2,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    4373 TSF:MSG:SEND,2-2-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    4381 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    4648 TSF:MSG:READ,0-0-2,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    4656 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=11,pt=0,l=22,sg=0,ft=0,st=OK:Distance+Motion Sensor
    4666 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
    4674 TSF:MSG:SEND,2-2-0-0,s=1,c=0,t=15,pt=0,l=0,sg=0,ft=0,st=OK:
    4715 !TSF:MSG:SEND,2-2-0-0,s=2,c=0,t=15,pt=0,l=0,sg=0,ft=0,st=NACK:
    4727 TSF:MSG:SEND,2-2-0-0,s=3,c=0,t=1,pt=0,l=0,sg=0,ft=1,st=OK:
    4733 MCO:REG:REQ
    4770 !TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=NACK:2
    6779 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=1,st=OK:2
    6848 TSF:MSG:READ,0-0-2,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    6852 MCO:PIM:NODE REG=1
    6855 MCO:BGN:STP
    6856 MCO:BGN:INIT OK,TSP=1
    1
    6860 TSF:MSG:SEND,2-2-0-0,s=3,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
    Ping Sonar A: 0 cm
    Ping Sonar B: 15 cm
    6970 TSF:MSG:SEND,2-2-0-0,s=2,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:15
    Ping Sonar A: 123 cm
    6985 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:123
    Ping Sonar B: 15 cm
    Ping Sonar A: 126 cm
    7102 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:126
    Ping Sonar B: 15 cm
    Ping Sonar A: 125 cm
    7222 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:125
    Ping Sonar B: 15 cm
    Ping Sonar A: 125 cm
    Ping Sonar B: 15 cm
    Ping Sonar A: 126 cm
    7450 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:126
    Ping Sonar B: 15 cm
    Ping Sonar A: 125 cm
    7569 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:125
    Ping Sonar B: 15 cm
    Ping Sonar A: 125 cm
    Ping Sonar B: 15 cm
    Ping Sonar A: 125 cm
    Ping Sonar B: 15 cm
    Ping Sonar A: 124 cm
    35517 TSF:MSG:SEND,2-2-0-0,s=2,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:78
    Ping Sonar A: 80 cm
    35544 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:80
    Ping Sonar B: 74 cm
    35656 TSF:MSG:SEND,2-2-0-0,s=2,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=OK:74
    Ping Sonar A: 132 cm
    35707 !TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=13,pt=2,l=2,sg=0,ft=0,st=NACK:132
    Ping Sonar B: 79 cm
    ...
    35820 TSF:MSG:SEND,2-2-0-0,s=2,c=1,t=13,pt=2,l=2,sg=0,ft=1,st=OK:79
    35827 MCO:SLP:MS=200,SMS=0,I1=255,M1=255,I2=255,M2=255
    35831 MCO:SLP:TPD
    35833 MCO:SLP:WUP=-1
    35835 MCO:SLP:MS=120000,SMS=0,I1=0,M1=1,I2=255,M2=255
    35841 MCO:SLP:TPD
    

  • Mod

    @Cliff-Karlsson ok, now that I am at a computer and could format the sketch to make it readable there are some things that seem really strange.

    #define INTERRUPT MOTION_A_TRIGGER_PIN-3 // Usually the interrupt = pin -2 (on uno/nano anyway)
    

    What is this about? Why -3?

    send(MOTION_A_msg.set(tripped ? "1" : "0")); // Send tripped value to gw
    

    This code sends the value of tripped every time the loop is executed. Is this your intention?
    EDIT: ok, this is your intention. Then it is fine.

    The main problem seems to be the -3, unless there is some reason that I'm not aware of. My recommendation is to use digitalPinToInterrupt (reference) instead.



  • @mfalkvidd Thanks for the help.
    I don't know why I had "INTERRUPT MOTION_A_TRIGGER_PIN-3" I think I originaly used pin2 for the motion sensor and when moving it to pin 3 I misunderstood and also changed that part from -2 to -3.

    Now most parts works as intended. But after triggering the motion sensor the distance-sensors fires for around 60s (Maybe the time it takes for the PIR to reset itself?)

    I only want the distance sensors to be active for 3s (unsigned long time = millis() + 30000;). How to I fix that?



  • I never understod how the millis() -part worked so I just changed it to a simple countdown instead.


Log in to reply
 

317
Online

6.7k
Users

7.6k
Topics

80.3k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.