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. Can you help spot my sketch mistake? Why's it ignoring the delay?

Can you help spot my sketch mistake? Why's it ignoring the delay?

Scheduled Pinned Locked Moved Troubleshooting
2 Posts 2 Posters 625 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.
  • Mark SwiftM Offline
    Mark SwiftM Offline
    Mark Swift
    wrote on last edited by Mark Swift
    #1

    Hi guys,

    I'm trying to get a simple node working, it's based on the moisture example. I've basically added another sensor in, which will be a rain sensor. I'm new to Arduino, but thought this would be fairly easy... :-/

    I presume I've made a simple mistake as it seems to avoid the delay between the 2 sensor readings?

    #define MY_NODE_ID 4
    // #define MY_PARENT_NODE_ID 2
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    // #define MY_RADIO_RFM69
    
    #define MY_RF24_CHANNEL 125
    
    #include <SPI.h>
    #include <MySensor.h>
    
    #define MOISTURE_POWER_PIN 4
    #define DIGITAL_INPUT_MOISTURE 5   // Digital input did you attach your soil sensor.  
    
    #define RAIN_POWER_PIN 6
    #define DIGITAL_INPUT_RAIN 7   // Digital input did you attach your soil sensor.  
    
    unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) 30000
    
    #define CHILD_ID0 0  // ID of the sensor child
    #define CHILD_ID1 1  // ID of the sensor child
    
    MyMessage msg1(CHILD_ID0, V_TRIPPED);
    MyMessage msg2(CHILD_ID1, V_TRIPPED);
    
    int lastMoistureValue = -1;
    int lastRainValue = -1;
    
    void setup()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Rn+M+T+H", "1.0");
      // Set the soil sensor digital pin as input
      pinMode(DIGITAL_INPUT_MOISTURE, INPUT);
      // Set the rain sensor digital pin as input
      pinMode(DIGITAL_INPUT_RAIN, INPUT);
      // Set the soil sensor power pin as output
      pinMode(MOISTURE_POWER_PIN, OUTPUT);
      // Set the rain sensor power pin as output
      pinMode(RAIN_POWER_PIN, OUTPUT);
      // Set to LOW so no power is flowing through the moisture sensor
      digitalWrite(MOISTURE_POWER_PIN, LOW);
      // Set to LOW so no power is flowing through the rain sensor
      digitalWrite(RAIN_POWER_PIN, LOW);
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID0, S_MOTION);
      present(CHILD_ID1, S_MOTION);
    }
    
    void loop()
    {
      // Turn moisture power pin on
      digitalWrite(MOISTURE_POWER_PIN, HIGH);
      // Set a delay to ensure the moisture sensor has taken a good reading
      delay(200);
      // Read digital soil value
      int moistureValue = digitalRead(DIGITAL_INPUT_MOISTURE); // 1 = Not triggered, 0 = In soil with water
      // Turn moisture power pin off
      digitalWrite(MOISTURE_POWER_PIN, LOW);
      if (moistureValue != lastMoistureValue)
      {
        #ifdef MY_DEBUG
        Serial.println("Moisture Sensor");
        Serial.println(moistureValue);
        #endif
        send(msg1.set(moistureValue == 0 ? 1 : 0)); // Send the inverse to gw as tripped should be when no water in soil
        lastMoistureValue = moistureValue;
      }
    
      // Wait between sensor readings, seems to help reliability of second reading
      delay(5000);
    
      // Turn rain power pin on
      digitalWrite(RAIN_POWER_PIN, HIGH);
      // Set a delay to ensure the moisture sensor has taken a good reading
      delay(200);
      // Read digital rain value
      int rainValue = digitalRead(DIGITAL_INPUT_RAIN); // 1 = Not triggered, 0 = In soil with water
      // Turn rain power pin off
      digitalWrite(RAIN_POWER_PIN, LOW);
      if (rainValue != lastRainValue)
      {
        #ifdef MY_DEBUG
        Serial.println("Rain Sensor");
        Serial.println(rainValue);
        #endif
        send(msg2.set(rainValue == 0 ? 1 : 0)); // Send the inverse to gw as tripped should be when no water in soil
        lastRainValue = rainValue;
      }
    
      // Sleep or wait depending on node role
      // sleep(SLEEP_TIME); // Sleep or wait (repeater)
    }
    
    1 Reply Last reply
    0
    • Boots33B Offline
      Boots33B Offline
      Boots33
      Hero Member
      wrote on last edited by Boots33
      #2

      That code looks ok to me. You should get a 5 second delay with that.
      Perhaps add

         Serial.println("start delay");
        // Wait between sensor readings, seems to help reliability of second reading
        delay(5000);
         Serial.println("finish delay");
      

      and see if you get your delay in the serial monitor```
      Insert Code Here

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


      24

      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