Can you help spot my sketch mistake? Why's it ignoring the delay?
-
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) }
-
That code looks ok to me. You should get a 5 second delay with that.
Perhaps addSerial.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