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. Add a second distance sensor to the same arduino?

Add a second distance sensor to the same arduino?

Scheduled Pinned Locked Moved Troubleshooting
18 Posts 3 Posters 3.7k 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 Cliff Karlsson

    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);
    }
    
    
    
    barduinoB Offline
    barduinoB Offline
    barduino
    wrote on last edited by
    #2

    @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 :smile:

    // 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);
    }
    
    1 Reply Last reply
    1
    • Cliff KarlssonC Offline
      Cliff KarlssonC Offline
      Cliff Karlsson
      wrote on last edited by
      #3

      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);
      }
      
      
      1 Reply Last reply
      0
      • barduinoB Offline
        barduinoB Offline
        barduino
        wrote on last edited by
        #4

        @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

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

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

          ?

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

            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);
            }
            
            TheoLT 1 Reply Last reply
            0
            • Cliff KarlssonC Cliff Karlsson

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

              ?

              TheoLT Offline
              TheoLT Offline
              TheoL
              Contest Winner
              wrote on last edited by TheoL
              #7

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

              1 Reply Last reply
              0
              • Cliff KarlssonC Cliff Karlsson

                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);
                }
                
                TheoLT Offline
                TheoLT Offline
                TheoL
                Contest Winner
                wrote on last edited by
                #8

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

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

                  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

                  TheoLT 2 Replies Last reply
                  0
                  • Cliff KarlssonC Cliff Karlsson

                    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

                    TheoLT Offline
                    TheoLT Offline
                    TheoL
                    Contest Winner
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • Cliff KarlssonC Cliff Karlsson

                      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

                      TheoLT Offline
                      TheoLT Offline
                      TheoL
                      Contest Winner
                      wrote on last edited by
                      #11

                      @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; 
                      
                      
                      1 Reply Last reply
                      0
                      • Cliff KarlssonC Offline
                        Cliff KarlssonC Offline
                        Cliff Karlsson
                        wrote on last edited by
                        #12

                        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);
                        //}
                        }
                        
                        TheoLT 1 Reply Last reply
                        0
                        • Cliff KarlssonC Cliff Karlsson

                          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);
                          //}
                          }
                          
                          TheoLT Offline
                          TheoLT Offline
                          TheoL
                          Contest Winner
                          wrote on last edited by
                          #13

                          @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

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

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

                            TheoLT 2 Replies Last reply
                            0
                            • Cliff KarlssonC Cliff Karlsson

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

                              TheoLT Offline
                              TheoLT Offline
                              TheoL
                              Contest Winner
                              wrote on last edited by
                              #15

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

                              1 Reply Last reply
                              0
                              • Cliff KarlssonC Cliff Karlsson

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

                                TheoLT Offline
                                TheoLT Offline
                                TheoL
                                Contest Winner
                                wrote on last edited by
                                #16

                                @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);
                                //}
                                }
                                
                                1 Reply Last reply
                                0
                                • Cliff KarlssonC Offline
                                  Cliff KarlssonC Offline
                                  Cliff Karlsson
                                  wrote on last edited by
                                  #17

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

                                  TheoLT 1 Reply Last reply
                                  0
                                  • Cliff KarlssonC Cliff Karlsson

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

                                    TheoLT Offline
                                    TheoLT Offline
                                    TheoL
                                    Contest Winner
                                    wrote on last edited by
                                    #18

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

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


                                    20

                                    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