Add a second distance sensor to the same arduino?



  • Hi, how do I add a second distance sensor to the original sketch using the developer-branch ?

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     * 
     * DESCRIPTION
     * This sketch provides an example how to implement a distance sensor using HC-SR04 
     * http://www.mysensors.org/build/distance
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensor.h>  
    #include <NewPing.h>
    
    #define CHILD_ID 1
    #define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     5  // 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 = 5000; // Sleep time between reads (in milliseconds)
    
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    MyMessage msg(CHILD_ID, V_DISTANCE);
    int lastDist;
    boolean metric = true; 
    
    void setup()  
    { 
      metric = getConfig().isMetric;
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Distance Sensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_DISTANCE);
    }
    
    void loop()      
    {     
      int dist = metric?sonar.ping_cm():sonar.ping_in();
      Serial.print("Ping: ");
      Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println(metric?" cm":" in");
    
      if (dist != lastDist) {
          send(msg.set(dist));
          lastDist = dist;
      }
    
      sleep(SLEEP_TIME);
    }
    
    
    


  • @Cliff-Karlsson

    It should be something like this

    Define new child id and pins for the new sensor

    #define SONAR_A_CHILD_ID 1
    #define SONAR_B_CHILD_ID 2
    #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 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.
    

    Then instantiate 2 sonars

    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.
    
    

    Now instantiate 2 messages and 2 lastDistance variables

    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;
    

    Presenting the sensors

    // 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);
    

    Finally the loop section

    void loop()      
    {     
      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;
      }
    
    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);
    }
    

    Other considerations:

    • Since sonar uses sound it might be a good idea to put some wait between the pings

    I did not test this but it did compile 😄

    // Enable debug prints
    #define MY_DEBUG
    
    // 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 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 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 = 5000; // Sleep time between reads (in milliseconds)
    
    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;
    
    boolean metric = true; 
    
    void setup()  
    { 
      metric = getConfig().isMetric;
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Distance 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);
    }
    
    void loop()      
    {     
      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);
    }
    


  • Thanks for the help, last question witch complicates the whole thing some more.

    If I want to also have two PIRs connected to the same arduino and want to use both PIRs as interrupts so that the sensor sleeps until one of the PIRs is activated.

    When the sensor wakes up after PIR movement PIRs should be put to sleep for 1min and the distance sensors would start.

    This is the Motion sketch:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Motion Sensor example using HC-SR501 
     * http://www.mysensors.org/build/motion
     *
     */
    
    // Enable debug prints
    // #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensor.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 1   // Id of the sensor child
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Motion Sensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_MOTION);
    }
    
    void loop()     
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
     
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    }
    
    


  • @Cliff-Karlsson

    The "trick" is this line

    // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    

    You can use something like this

    int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0);
    

    Take a look in the MySensors API page under "sleeping" http://www.mysensors.org/download/sensor_api_15

    Cheers



  • @barduino said:

    int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0);

    Ok so if PIRs are connected to PIN 10 and PIN 11 I would use:

    int8_t sleep(uint8_t 10, uint8_t CHANGE, uint8_t 11, uint8_t CHANGE, unsigned long ms=0); 
    

    ?



  • Ok I got the two distance sensors to work and now I am trying to also add a motion sensor. First question, I read in the motion sensor sketch that only pin 2 and 3 generates interrupts and my radio is connected to pin 2 already. But I also recall reading something about pin 2 on the radio is not used so that means that I can use pin 2 and 3 for the PIRs, right?

    This is what I have come up with for now but I know there is some errors in it and I have not quite figured out the sleep part yet.

    And in the loop do I use ?

    if(tripped){"do distance stuff"}
    

    This is what I have go so far I added some white lines to the right of the parts that I added to the working distance sketch:

    // 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 = 5000; // 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 msg(MOTION_A, 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(DIGITAL_INPUT_SENSOR) == HIGH; ------
      
        
      Serial.println(tripped); -----
      send(msg.set(tripped?"1":"0"));  // Send tripped value to gw  -----
     
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      sleep(INTERRUPT,CHANGE, SLEEP_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(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);
    }
    

  • Contest Winner

    @Cliff-Karlsson You can only use pin2 and pin3 as interrupt pins. You don't need to connect the NRF24L01+ to pin 2. So you can use both. I'm doing this with my doorbell works perfect.

    Notice. If you're node also receives messages from the gateway, you wont be able to put the node to sleep. MySensors currently doesn't support wake up on message arrival. That's why pin2 can be used for connecting sensors.

    I hope this makes sense 😉


  • Contest Winner

    @Cliff-Karlsson I'm not sure if you're aware of this. But in your setup, you need to start the MySensors communication. Like the gw.begin() and the presentation of the child's.

    I don't see this in your code:

    void setup()  
    { 
      metric = getConfig().isMetric;
      pinMode(MOTION_A_TRIGGER_PIN, INPUT);      // sets the motion sensor digital pin as input ----------------------
    }
    

    No offence. Just trying to help you.



  • Absolutley none offence taken, I really suck at coding and my main methods are copy/paste and click on verify to check if it worked.

    I am using the developer branch that does not use the gw. stuff if that is what you mean?

    This is what I have tried for now but ofcourse it does not work.

    // 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 msg(MOTION_A_msg, 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(DIGITAL_INPUT_SENSOR) == HIGH; 
      
        
      Serial.println(tripped);
      send(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);
    }
    }
    

    Error: 'MOTION_A_msg' was not declared in this scope


  • Contest Winner

    @Cliff-Karlsson I'm sorry I haven't played with the development branch. I'm just using 1.5.4. But the part abount interrupts and pin2 and 3 stays the same. It's just how the Arduino works.


  • Contest Winner

    @Cliff-Karlsson I think you want this:

    MyMessage msg(MOTION_A_CHILD_ID, V_TRIPPED);
    
    boolean metric = true; 
    

    instead of

    MyMessage msg(MOTION_A_msg, V_TRIPPED);
    
    boolean metric = true; 
    
    


  • Thanks, sorry fore code-spamming but it feels like I am starting to get close to the first step witch is a succesful verify.
    The whole sketch verifies ok until it gets to the last "sleep" then I get this error:

    "expected constructor, destructor, or type conversion before '(' token"

    // 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);
    //}
    }
    

  • Contest Winner

    @Cliff-Karlsson To me that's what this community is about. I'll have a loop. But no promise, as said before I haven't played with the development branch yet. But I do understand the compiler error



  • @TheoL said:

    what this community is about. I'll have a loop. But no promise, as said before I haven't played with the development branch yet. But I do understand the compiler error

    Thanks.
    And if unclear I am trying to achieve in the end is:

    When PIR senses motion, activate both distance sensors for 30 seconds and deactivate PIR.
    After 30 seconds deactivate distance sensors and activate PIR again.


  • Contest Winner

    @Cliff-Karlsson Is it giving the error on the first or the second sleep?

    I have to compliment @Hek and the other guys. They've made MySensors easier to program. The branch looks really great. Love it.


  • Contest Winner

    @Cliff-Karlsson I think I found it.

    it's in this part

    
        sleep(SLEEP_TIME);
      }
                } // <--------- this ends the loop() method
    //else{
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); // <---- so this is not in the method
    //}
    } // <-------- int this is also outside of the loop method
    

    Change it to:

    
        sleep(SLEEP_TIME);
      }
     //           }  // comment brace
    //else{
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME);
    //}
    }
    


  • After commenting the bracket I get : 'DIGITAL_INPUT_SENSOR' was not declared in this scope


  • Contest Winner

    @Cliff-Karlsson I'm sorry would love help you further. But it's getting late over here and my better half wants me to turn off the laptop.


Log in to reply
 

Suggested Topics

  • 3
  • 8
  • 1
  • 6
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts