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. Development
  3. Customizable Sonar Sensor

Customizable Sonar Sensor

Scheduled Pinned Locked Moved Development
4 Posts 2 Posters 1.9k Views 2 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.
  • B Offline
    B Offline
    beanl
    wrote on last edited by
    #1

    Hi,

    I would like to build a customizable node (customizable by the controller) wich can detect presence for a certain distance.
    The node comportement is good...

    • Is it good practice to do a sketch like this ?
    • "gw.request(CHILD_ID_SONAR_MIN_RANGE, V_DISTANCE);", response is provided by the controller ?
    • Can i store my minimum range distance in EEPROM ? or is it better to do a "gw.request" ?
    #include <SPI.h>
    #include <MySensor.h>  
    #include <NewPing.h>
    
    
    #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.
    /** Default value **/
    #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)
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    // Id of the sensor child
    #define CHILD_ID_SONAR 0
    #define CHILD_ID_SONAR_MIN_RANGE 1
    
    
    //Setting default min / max range default value
    int minRange = 50;
    boolean inRange = false;
    boolean outRange = false;
    
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    
    MyMessage SonarMessage(CHILD_ID_SONAR, V_DISTANCE);
    
    unsigned long lastSend; // the timeStamp you use to constantly check vs the current time (millis() returns an unsigned long timestamp)
    unsigned long IntervalBetweenSend = 2000; // the frequency that this timer will execute here... 10 seconds.
    
    boolean metric = true; 
    
    
    void setup()  
    { 
      gw.begin(incomingMessage, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Distance Sensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(0, S_DISTANCE);
      
      // Presenting custom 
      gw.present(CHILD_ID_SONAR_MIN_RANGE, S_DISTANCE); //minRange
      
      // Request/wait for dimmer status
      //gw.request(CHILD_ID_SONAR_MIN_RANGE, V_VAR1);
      gw.request(CHILD_ID_SONAR_MIN_RANGE, V_DISTANCE);
      //gw.process();
      //gw.request(CHILD_ID_SONAR_MAX_RANGE, V_DISTANCE);
      //gw.process();
      //gw.saveState(1,2);
      
      boolean metric = gw.getConfig().isMetric;
    }
    
    void loop(){
      
      // Process incomming message
      gw.process();
      
      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 (millis() - lastSend > IntervalBetweenSend) {
        
        int dist = metric?sonar.ping_cm():sonar.ping_in();
            
          if ( dist <= minRange && inRange == false) {
              
                gw.send(SonarMessage.set(dist));
                inRange = true;
                lastSend = millis();
              
          }
          
          if ( dist > minRange && inRange == true) {
             gw.send(SonarMessage.set(dist));
             inRange = false;
             lastSend = millis();
          } 
        
        
      }
    
    }
    
    void incomingMessage(const MyMessage &message) {
      // echo "1;1;2;1;13;40" >> /dev/ttyUSB0 (Defining minrange to 40)
      // echo "1;2;2;1;13;50" >> /dev/ttyUSB0 (Defining maxrange to 50)
       Serial.print("Incoming Message");
       
      // We wait for V_VAR1 value
      if (message.type == V_DISTANCE) {
         // Store state in eeprom
         //gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(" (type : ");
         Serial.print(message.type);
         Serial.print(")");
         Serial.print(", New status: ");
         Serial.println(message.getInt());
         
         if ( message.sensor == CHILD_ID_SONAR_MIN_RANGE ){
           Serial.println("Updating Minrange");
           minRange = message.getInt();
         }  
         
         
       } 
    }```
    1 Reply Last reply
    0
    • B Offline
      B Offline
      beanl
      wrote on last edited by
      #2

      can someone can just answer this question :
      "gw.request(CHILD_ID_SONAR_MIN_RANGE, V_DISTANCE);", response is provided by the controller ?

      thank you :)

      S 1 Reply Last reply
      0
      • B beanl

        can someone can just answer this question :
        "gw.request(CHILD_ID_SONAR_MIN_RANGE, V_DISTANCE);", response is provided by the controller ?

        thank you :)

        S Offline
        S Offline
        Sparkman
        Hero Member
        wrote on last edited by
        #3

        @beanl said:

        gw.request(

        Yes, the controller is supposed to provide a response. However, not all controllers currently support that.

        Cheers
        Al

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beanl
          wrote on last edited by
          #4

          ok thank you :)

          1 Reply Last reply
          0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          12

          Online

          12.1k

          Users

          11.2k

          Topics

          113.4k

          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