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. Can it be done? (Countdown timer that survives wait or sleep)

Can it be done? (Countdown timer that survives wait or sleep)

Scheduled Pinned Locked Moved Development
21 Posts 6 Posters 6.0k Views 4 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 Mark Swift

    Hi Guys,

    I'm trying to add a simple countdown timer into my sketch. the logic is, if x is true, reset the timer, if it isn't start the timer.

    Basically, if rain is detected, reset the timer. If it isn't countdown from 4 hours.

    I tried a library that I found but it didn't seem to work with the wait or sleep command (which I expected).

    Any ideas?

    AWIA Offline
    AWIA Offline
    AWI
    Hero Member
    wrote on last edited by AWI
    #9

    @Mark-Swift The easiest way to implement a time during sleep (independent from a controller) is count the number of sleep and multiply by the sleep time.

    or a more straightforward example:

    unsigned long loopTimer ; 
    
    void setup()
    {
    loopTimer  = 0 ;
    }
    
    void loop(){
    unsigned long now = millis() ;
    // whatever 
    
    // end whatever
    loopTimer += now ; 
    gw.sleep(sleepTime);
    loopTimer += sleepTime ;
    }
    
    1 Reply Last reply
    1
    • Mark SwiftM Offline
      Mark SwiftM Offline
      Mark Swift
      wrote on last edited by Mark Swift
      #10

      @AWI That's very interesting. I had considered something like that but couldn't figure out how I'd do it.

      Basically, what I'd like to do is have the Arduino light up the neopixel with the remaining hours, i.e. 4 hours, show 4 red LEDS. Not having to reply on a controller for such a simple task seems a bonus. If possible I'll also send the remaining time back to the controller also.

      Now I just need to figure out how to apply your timer example to my code, oh dear.

      @AWI If you're feeling generous ;-)

      AWIA 1 Reply Last reply
      0
      • Mark SwiftM Mark Swift

        @AWI That's very interesting. I had considered something like that but couldn't figure out how I'd do it.

        Basically, what I'd like to do is have the Arduino light up the neopixel with the remaining hours, i.e. 4 hours, show 4 red LEDS. Not having to reply on a controller for such a simple task seems a bonus. If possible I'll also send the remaining time back to the controller also.

        Now I just need to figure out how to apply your timer example to my code, oh dear.

        @AWI If you're feeling generous ;-)

        AWIA Offline
        AWIA Offline
        AWI
        Hero Member
        wrote on last edited by
        #11

        @Mark-Swift I'm feeling generous... but the challenge is yours...:laughing: .

        If a precision of a few milliseconds is not essential then my idea would be to implement a counter which increments every loop.
        If the counter reaches 1 hour (3600 s equals ? times the loopDelay) light up the first, etc.

        1 Reply Last reply
        0
        • Mark SwiftM Offline
          Mark SwiftM Offline
          Mark Swift
          wrote on last edited by Mark Swift
          #12
          This post is deleted!
          1 Reply Last reply
          0
          • Mark SwiftM Offline
            Mark SwiftM Offline
            Mark Swift
            wrote on last edited by
            #13

            Well, I've been trying and haven't come up with anything that works so far... Crap.

            1 Reply Last reply
            0
            • m26872M Offline
              m26872M Offline
              m26872
              Hardware Contributor
              wrote on last edited by
              #14

              It looks like you don't need to sleep, i.e. the node isn't battery powered. Then it should be very easy by using the basic millis() counter method. http://forum.mysensors.org/topic/630/gateway-sensor-range-advice/12

              1 Reply Last reply
              0
              • Mark SwiftM Offline
                Mark SwiftM Offline
                Mark Swift
                wrote on last edited by Mark Swift
                #15

                @m26872 Would you perhaps be able to point me in the right direction with regards my sketch?

                Basically, if

                if (lastMoistureValue == 0 ? 1 : 0 == 1 || lastRainValue == 0 ? 1 : 0 == 1)
                

                reset timer and set MowerWaiting to true (I guess?)

                else

                after 1 hour light led 1, after 2 hours light led 2, after 3 hours light led 3, after 4 hours light led 4 (and ideally switch MowerWaiting to false)

                Cheers, Mark

                TheoLT 1 Reply Last reply
                0
                • Mark SwiftM Mark Swift

                  @m26872 Would you perhaps be able to point me in the right direction with regards my sketch?

                  Basically, if

                  if (lastMoistureValue == 0 ? 1 : 0 == 1 || lastRainValue == 0 ? 1 : 0 == 1)
                  

                  reset timer and set MowerWaiting to true (I guess?)

                  else

                  after 1 hour light led 1, after 2 hours light led 2, after 3 hours light led 3, after 4 hours light led 4 (and ideally switch MowerWaiting to false)

                  Cheers, Mark

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

                  @Mark-Swift Why not make things simple (I love simple things).

                  if (lastMoistureValue == 0 ? 1 : 0 == 1 || lastMoistureValue == 0 ? 1 : 0 == 1)
                  

                  Is something that I would code differently. What you are saying is:
                  if ( inverted value of lastMoistureValue == 1 <-- meaning moisturevalue = 0

                  or

                  if ( inverted value of lastMoisterValue == 1 <-- meaning moistureValue = 0

                  Which basically is a double check for the same. If you'd want to do something when the lastMoistureValue = 0 it would be more logic and simple to say

                  if ( lastMoistureValue == 0 ) {
                  

                  But I don't have the time to read this complete thread. To me it always help to write things in pseudo code first. Especially when I have to code a complex algorithm. Like

                  1. if the lastMoistureValue is off (I presume 0 is off) then do
                  2. ....

                  And then I translate the pseudo code to C code.

                  An algorithm for handling timers could be:

                  1. If something/sensor value starts a timer.
                  2. store current time in timerStartVariable
                  3. if firstTimerElapsed
                  4. light up led1
                  5. if secondTimerElapsed
                  6. light up led2
                  7. ...... and so on
                  8. if timer needs reset
                  9. disable timer mechanism

                  This gives me lots of insights in what I'd need to code. And there are a lot of different ways you can accomplish this. But for the sake of simplicity I'm not going to use arrays and stuff like that. That is something you can do later on - I'd do immediately, but I wrote my first program 36 years ago on a commodore 64. So I have some experience with software development.

                  #define firstTimer 36000000 // 60 minutes * 60 seconds * 1000 millis
                  #define secondTimer 7200000 // 2 times first timer is 2 hours
                  .... // and so on
                  
                  
                  bool timerActive =  false; // state for timer active
                  unsigned long timerStart = 0; // variable for remembering when the timer was triggered
                  bool timer1Handled = false;
                  bool timer2Handled = false;
                  ....
                  ....
                  void loop() {
                      if ( timerActive == false && timerNeedsToBeActivated ) { // don't know how you want to trigger it
                          timerActive = true;
                          timerStart = millis();
                          timer1Handled = false;
                          timer2Handled = false;
                      }
                      if ( timerActive == true ) { // check whether a timer has ellapsed
                          unsigned long cm = millis();
                          if ( timerStart + firstTimer > cm && timer1Handled == false ) {
                              timer1Handled = true;
                              // light up led1
                          }
                          if ( timerStart + secondTimer > cm && timer2Handled == false ) {
                              timer2Handled = true;
                              // light up led1
                          }
                          if ( timerNeedsToBeStopped ) {
                              timerActive = false;
                          }
                      }
                  }
                  

                  Now this might not be the best way to code this algorithm. And I haven't checked if it compiles. But I hope this is what you're looking for. And if so I hope it helped the way I translate an algorithm to a piece of software.

                  Also there might be some small errors in the code. But I just did some development in Python for a course I'm following. And Python has a different syntax.

                  1 Reply Last reply
                  0
                  • Mark SwiftM Offline
                    Mark SwiftM Offline
                    Mark Swift
                    wrote on last edited by
                    #17

                    @Mark-Swift said:

                    @TheoL I'll take a look at that and digest it, thank you so much for taking the time to reply.

                    BTW, spot the typo! It was meant to read:

                    if (lastMoistureValue == 0 ? 1 : 0 == 1 || lastRainValue == 0 ? 1 : 0 == 1)
                    
                    TheoLT 1 Reply Last reply
                    0
                    • Mark SwiftM Mark Swift

                      @Mark-Swift said:

                      @TheoL I'll take a look at that and digest it, thank you so much for taking the time to reply.

                      BTW, spot the typo! It was meant to read:

                      if (lastMoistureValue == 0 ? 1 : 0 == 1 || lastRainValue == 0 ? 1 : 0 == 1)
                      
                      TheoLT Offline
                      TheoLT Offline
                      TheoL
                      Contest Winner
                      wrote on last edited by
                      #18

                      @Mark-Swift My mistake sorry. But it would also make the code more readable by not doing the invert and just compare for the value.

                      1 Reply Last reply
                      0
                      • Mark SwiftM Offline
                        Mark SwiftM Offline
                        Mark Swift
                        wrote on last edited by Mark Swift
                        #19

                        @TheoL I've just tried implementing the code you gave into my sketch, strangely it seems to light all 3 LEDS on the first loop, even though checking the value of 'cm' it's obviously less than timerStarted + TIMERX, any ideas?

                        Code:

                        // Enable debug prints
                        #define MY_DEBUG
                        
                        // Required for MQTT as gateway does not automagically assign ID's
                        #define MY_NODE_ID 3
                        
                        // Set parent node ID manually
                        // #define MY_PARENT_NODE_ID
                        
                        // Enable and select radio type attached
                        #define MY_RADIO_NRF24
                        // #define MY_RADIO_RFM69
                        
                        // Set RF24L01 channel number
                        #define MY_RF24_CHANNEL 125
                        
                        #include <SPI.h>
                        #include <MySensor.h>
                        #include <BH1750.h>
                        #include <NewPing.h>
                        #include <Adafruit_NeoPixel.h>
                        
                        #define NEO_PIN 2
                        #define NUM_LEDS 8
                        Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUM_LEDS, NEO_PIN, NEO_GRB + NEO_KHZ800);
                        
                        // Power pin moisture sensor
                        #define MOISTURE_POWER_PIN 5
                        // Digital input pin moisture sensor
                        #define DIGITAL_INPUT_MOISTURE 6
                        
                        // Power pin rain sensor
                        #define RAIN_POWER_PIN 7
                        // Digital input pin rain sensor
                        #define DIGITAL_INPUT_RAIN 8
                        
                        // Ultrasonic trigger pin
                        #define TRIGGER_PIN 4
                        // Ultrasonic echo pin
                        #define ECHO_PIN 3
                        // Maximum distance we want to ping for (in cms), maximum sensor distance is rated at 400-500cm
                        #define MAX_DISTANCE 400
                        // NewPing setup of pins and maximum distance
                        NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
                        
                        // Set BH1750 name
                        BH1750 lightSensor;
                        
                        // Define end of loop pause time
                        #define LOOP_PAUSE 10000
                        
                        // Define radio wait time between sends
                        #define RADIO_PAUSE 100  // This allows for radio to come back to power after a transmission, ideally 0..
                        
                        // Define time between sensors blocks
                        #define SENSORS_DELAY 100  // This allows for radio to come back to power after a transmission, ideally 0..
                        
                        // Define delay set on Landroid
                        #define MOWER_DELAY (4UL*60UL*60UL*1000UL)  // Hours (4)
                        boolean MowerWaiting = false; // If false, send relay command
                        boolean MowerHome = false; // If false, send relay command
                        
                        // unsigned long LOOP_PAUSE = 10000; // Sleep or wait time between reads (in milliseconds) 30000
                        int lastMoistureValue = -1;
                        int lastRainValue = -1;
                        uint16_t lastlux;
                        int lastDist;
                        boolean metric = true;
                        
                        // Trial timer variables
                        
                        #define TIMER1 3200000 // 60 minutes * 60 seconds * 1000 millis
                        #define TIMER2 7200000 // 120 minutes * 60 seconds * 1000 millis
                        #define TIMER3 10800000 // 180 minutes * 60 seconds * 1000 millis
                        
                        bool timerActive =  false; // State for timer active
                        unsigned long timerStarted = 0; // Variable for remembering when the timer was last triggered
                        bool timer1Activated = false;
                        bool timer2Activated = false;
                        bool timer3Activated = false;
                        
                        // Define sensor children for MySensors
                        
                        #define CHILD_ID1 1 // ID of the sensor child
                        #define CHILD_ID2 2 // ID of the sensor child
                        #define CHILD_ID3 3 // ID of the sensor child
                        #define CHILD_ID4 4 // ID of the sensor child
                        
                        // Define MySensors message formats
                        
                        MyMessage msg1(CHILD_ID1, V_TRIPPED); // Setup message
                        MyMessage msg2(CHILD_ID2, V_TRIPPED); // Setup message
                        MyMessage msg3(CHILD_ID3, V_LEVEL); // Setup message
                        MyMessage msg4(CHILD_ID4, V_DISTANCE); // Setup message
                        
                        void setup()
                        {
                          // Send the sketch version information to the gateway and Controller
                          sendSketchInfo("R+M+T+H+L+D+U", "1.0");
                          wait(RADIO_PAUSE);
                          // Register all sensors to gw (they will be created as child devices)
                          present(CHILD_ID1, S_MOTION);
                          wait(RADIO_PAUSE);
                          present(CHILD_ID2, S_MOTION);
                          wait(RADIO_PAUSE);
                          present(CHILD_ID3, S_LIGHT_LEVEL);
                          wait(RADIO_PAUSE);
                          present(CHILD_ID4, S_DISTANCE);
                          wait(RADIO_PAUSE);
                          // Set the moisture 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 moisture 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);
                          // Check gateway for metric setting
                          boolean metric = getConfig().isMetric;
                          // Start BH1750 light sensor
                          lightSensor.begin();
                          // Start NeoPixel
                          strip1.begin();
                          // Initialize all pixels to 'off'
                          strip1.show();
                        }
                        
                        void loop()
                        {
                          {
                            //--- Moisture sensor ---//
                            digitalWrite(MOISTURE_POWER_PIN, HIGH); // Turn moisture power pin on
                            wait(200); // Set a delay to ensure the moisture sensor has powered up
                            int moistureValue = digitalRead(DIGITAL_INPUT_MOISTURE); // Read digital moisture value
                            digitalWrite(MOISTURE_POWER_PIN, LOW); // Turn moisture power pin off
                            if (moistureValue != lastMoistureValue)
                            {
                        #ifdef MY_DEBUG
                              Serial.print("Moisture Sensor: ");
                              Serial.println(moistureValue == 0 ? 1 : 0);
                        #endif
                              send(msg1.set(moistureValue == 0 ? 1 : 0)); // Send the inverse
                              wait(RADIO_PAUSE);
                              lastMoistureValue = moistureValue; // For testing can be 0 or 1 or back to moistureValue
                            }
                          }
                        
                          wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
                          {
                            //--- Rain sensor ---//
                            digitalWrite(RAIN_POWER_PIN, HIGH); // Turn rain power pin on
                            wait(200); // Set a delay to ensure the moisture sensor has powered up
                            int rainValue = digitalRead(DIGITAL_INPUT_RAIN); // Read digital rain value
                            digitalWrite(RAIN_POWER_PIN, LOW); // Turn rain power pin off
                            if (rainValue != lastRainValue) // Check value against saved value
                            {
                        #ifdef MY_DEBUG
                              Serial.print("Rain Sensor: ");
                              Serial.println(rainValue == 0 ? 1 : 0);
                        #endif
                              send(msg2.set(rainValue == 0 ? 1 : 0)); // Send the inverse
                              wait(RADIO_PAUSE);
                              lastRainValue = rainValue; // For testing can be 0 or 1 or back to rainValue
                            }
                          }
                        
                          wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
                          {
                            //--- Light sensor ---//
                            uint16_t lux = lightSensor.readLightLevel(); // Get Lux value
                            if (lux != lastlux)
                            {
                        #ifdef MY_DEBUG
                              Serial.print("LUX: ");
                              Serial.println(lux);
                        #endif
                              send(msg3.set(lux));
                              wait(RADIO_PAUSE);
                              lastlux = lux;
                            }
                          }
                        
                          wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
                          {
                            //--- Distance sensor ---//
                            int dist = metric ? sonar.ping_cm() : sonar.ping_in();
                            if (dist != lastDist)
                            {
                        #ifdef MY_DEBUG
                              Serial.print("Distance: ");
                              Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
                              Serial.println(metric ? " cm" : " in");
                        #endif
                              send(msg4.set(dist));
                              wait(RADIO_PAUSE);
                              lastDist = dist;
                            }
                          }
                        
                          //--- Analyse readings and set neopixel & booleans ---//
                          if (lastMoistureValue == 0 || lastRainValue == 0)
                          {
                            MowerWaiting = true;
                        #ifdef MY_DEBUG
                            Serial.print("Mower Waiting: ");
                            Serial.println(MowerWaiting);
                        #endif
                          }
                        
                          if ( timerActive == false && MowerWaiting == true ) // Logic for timer trigger
                          {
                            timerActive = true;
                            timerStarted = millis();
                            timer1Activated = false;
                            timer2Activated = false;
                            timer3Activated = false;
                            strip1.setPixelColor(0, 255, 0, 0);
                            strip1.setPixelColor(1, 255, 0, 0);
                            strip1.setPixelColor(2, 255, 0, 0);
                            strip1.setPixelColor(3, 255, 0, 0);
                            strip1.show();
                        #ifdef MY_DEBUG
                            Serial.print("Mower Is Waiting: ");
                            Serial.println("Activating Countdown");
                        #endif
                          }
                        
                          if ( timerActive == true )
                          {
                        #ifdef MY_DEBUG
                            Serial.print("Countdown: ");
                            Serial.println("Active");
                        #endif
                            unsigned long cm = millis();
                            if ( timerStarted + TIMER1 < cm && timer1Activated == false )
                            {
                              timer1Activated = true;
                              strip1.setPixelColor(0, 255, 0, 0);
                              strip1.setPixelColor(1, 255, 0, 0);
                              strip1.setPixelColor(2, 255, 0, 0);
                              strip1.setPixelColor(3, 0, 0, 0);
                              strip1.show();
                        #ifdef MY_DEBUG
                              Serial.print("3 Hours Left: ");
                              Serial.println("");
                        #endif
                            }
                            if ( timerStarted + TIMER2 < cm && timer2Activated == false )
                            {
                              timer2Activated = true;
                              strip1.setPixelColor(0, 255, 0, 0);
                              strip1.setPixelColor(1, 255, 0, 0);
                              strip1.setPixelColor(2, 0, 0, 0);
                              strip1.setPixelColor(3, 0, 0, 0);
                              strip1.show();
                        #ifdef MY_DEBUG
                              Serial.print("2 Hours Left: ");
                              Serial.println("");
                        #endif
                            }
                            if ( timerStarted + TIMER3 < cm && timer3Activated == false )
                            {
                              timer3Activated = true;
                              strip1.setPixelColor(0, 255, 0, 0);
                              strip1.setPixelColor(1, 0, 0, 0);
                              strip1.setPixelColor(2, 0, 0, 0);
                              strip1.setPixelColor(3, 0, 0, 0);
                              strip1.show();
                        #ifdef MY_DEBUG
                              Serial.print("1 Hours Left");
                              Serial.println("");
                        #endif
                            }
                            if ( cm > 14400000 )
                            {
                              timerActive = false;
                              strip1.setPixelColor(0, 0, 255, 0);
                              strip1.setPixelColor(1, 0, 255, 0);
                              strip1.setPixelColor(2, 0, 255, 0);
                              strip1.setPixelColor(3, 0, 255, 0);
                              strip1.show();
                        #ifdef MY_DEBUG
                              Serial.print("Mower Is No Longer Waiting: ");
                              Serial.println("Stopping Timer");
                        #endif
                            }
                          }
                        
                          if (lastDist < 60)
                          {
                            MowerHome = true;
                        #ifdef MY_DEBUG
                            Serial.print("Mower Home: ");
                            Serial.println(MowerHome);
                            strip1.setPixelColor(4, 0, 127, 0);
                            strip1.setPixelColor(5, 0, 127, 0);
                            strip1.setPixelColor(6, 0, 127, 0);
                            strip1.setPixelColor(7, 0, 127, 0);
                            strip1.show();
                        #endif
                          }
                        
                          else
                          {
                            MowerHome = false;
                        #ifdef MY_DEBUG
                            Serial.print("Mower Home: ");
                            Serial.println(MowerHome);
                            strip1.setPixelColor(4, 255, 0, 0);
                            strip1.setPixelColor(5, 255, 0, 0);
                            strip1.setPixelColor(6, 255, 0, 0);
                            strip1.setPixelColor(7, 255, 0, 0);
                            strip1.show();
                        #endif
                          }
                        
                          wait(LOOP_PAUSE); // Sleep or wait (repeater)
                        }
                        

                        Edit: I'm guessing the IF command should be "<" rather than ">" , fixed that. Check the code above for what I'm working on.

                        The issue I see right now is that if either moisture or rain is detected the mower timer will be reset each loop (that's okay), how can I also reset the countdown timer you created that I've implemented so they're in sync?

                        TheoLT 1 Reply Last reply
                        0
                        • Mark SwiftM Mark Swift

                          @TheoL I've just tried implementing the code you gave into my sketch, strangely it seems to light all 3 LEDS on the first loop, even though checking the value of 'cm' it's obviously less than timerStarted + TIMERX, any ideas?

                          Code:

                          // Enable debug prints
                          #define MY_DEBUG
                          
                          // Required for MQTT as gateway does not automagically assign ID's
                          #define MY_NODE_ID 3
                          
                          // Set parent node ID manually
                          // #define MY_PARENT_NODE_ID
                          
                          // Enable and select radio type attached
                          #define MY_RADIO_NRF24
                          // #define MY_RADIO_RFM69
                          
                          // Set RF24L01 channel number
                          #define MY_RF24_CHANNEL 125
                          
                          #include <SPI.h>
                          #include <MySensor.h>
                          #include <BH1750.h>
                          #include <NewPing.h>
                          #include <Adafruit_NeoPixel.h>
                          
                          #define NEO_PIN 2
                          #define NUM_LEDS 8
                          Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUM_LEDS, NEO_PIN, NEO_GRB + NEO_KHZ800);
                          
                          // Power pin moisture sensor
                          #define MOISTURE_POWER_PIN 5
                          // Digital input pin moisture sensor
                          #define DIGITAL_INPUT_MOISTURE 6
                          
                          // Power pin rain sensor
                          #define RAIN_POWER_PIN 7
                          // Digital input pin rain sensor
                          #define DIGITAL_INPUT_RAIN 8
                          
                          // Ultrasonic trigger pin
                          #define TRIGGER_PIN 4
                          // Ultrasonic echo pin
                          #define ECHO_PIN 3
                          // Maximum distance we want to ping for (in cms), maximum sensor distance is rated at 400-500cm
                          #define MAX_DISTANCE 400
                          // NewPing setup of pins and maximum distance
                          NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
                          
                          // Set BH1750 name
                          BH1750 lightSensor;
                          
                          // Define end of loop pause time
                          #define LOOP_PAUSE 10000
                          
                          // Define radio wait time between sends
                          #define RADIO_PAUSE 100  // This allows for radio to come back to power after a transmission, ideally 0..
                          
                          // Define time between sensors blocks
                          #define SENSORS_DELAY 100  // This allows for radio to come back to power after a transmission, ideally 0..
                          
                          // Define delay set on Landroid
                          #define MOWER_DELAY (4UL*60UL*60UL*1000UL)  // Hours (4)
                          boolean MowerWaiting = false; // If false, send relay command
                          boolean MowerHome = false; // If false, send relay command
                          
                          // unsigned long LOOP_PAUSE = 10000; // Sleep or wait time between reads (in milliseconds) 30000
                          int lastMoistureValue = -1;
                          int lastRainValue = -1;
                          uint16_t lastlux;
                          int lastDist;
                          boolean metric = true;
                          
                          // Trial timer variables
                          
                          #define TIMER1 3200000 // 60 minutes * 60 seconds * 1000 millis
                          #define TIMER2 7200000 // 120 minutes * 60 seconds * 1000 millis
                          #define TIMER3 10800000 // 180 minutes * 60 seconds * 1000 millis
                          
                          bool timerActive =  false; // State for timer active
                          unsigned long timerStarted = 0; // Variable for remembering when the timer was last triggered
                          bool timer1Activated = false;
                          bool timer2Activated = false;
                          bool timer3Activated = false;
                          
                          // Define sensor children for MySensors
                          
                          #define CHILD_ID1 1 // ID of the sensor child
                          #define CHILD_ID2 2 // ID of the sensor child
                          #define CHILD_ID3 3 // ID of the sensor child
                          #define CHILD_ID4 4 // ID of the sensor child
                          
                          // Define MySensors message formats
                          
                          MyMessage msg1(CHILD_ID1, V_TRIPPED); // Setup message
                          MyMessage msg2(CHILD_ID2, V_TRIPPED); // Setup message
                          MyMessage msg3(CHILD_ID3, V_LEVEL); // Setup message
                          MyMessage msg4(CHILD_ID4, V_DISTANCE); // Setup message
                          
                          void setup()
                          {
                            // Send the sketch version information to the gateway and Controller
                            sendSketchInfo("R+M+T+H+L+D+U", "1.0");
                            wait(RADIO_PAUSE);
                            // Register all sensors to gw (they will be created as child devices)
                            present(CHILD_ID1, S_MOTION);
                            wait(RADIO_PAUSE);
                            present(CHILD_ID2, S_MOTION);
                            wait(RADIO_PAUSE);
                            present(CHILD_ID3, S_LIGHT_LEVEL);
                            wait(RADIO_PAUSE);
                            present(CHILD_ID4, S_DISTANCE);
                            wait(RADIO_PAUSE);
                            // Set the moisture 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 moisture 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);
                            // Check gateway for metric setting
                            boolean metric = getConfig().isMetric;
                            // Start BH1750 light sensor
                            lightSensor.begin();
                            // Start NeoPixel
                            strip1.begin();
                            // Initialize all pixels to 'off'
                            strip1.show();
                          }
                          
                          void loop()
                          {
                            {
                              //--- Moisture sensor ---//
                              digitalWrite(MOISTURE_POWER_PIN, HIGH); // Turn moisture power pin on
                              wait(200); // Set a delay to ensure the moisture sensor has powered up
                              int moistureValue = digitalRead(DIGITAL_INPUT_MOISTURE); // Read digital moisture value
                              digitalWrite(MOISTURE_POWER_PIN, LOW); // Turn moisture power pin off
                              if (moistureValue != lastMoistureValue)
                              {
                          #ifdef MY_DEBUG
                                Serial.print("Moisture Sensor: ");
                                Serial.println(moistureValue == 0 ? 1 : 0);
                          #endif
                                send(msg1.set(moistureValue == 0 ? 1 : 0)); // Send the inverse
                                wait(RADIO_PAUSE);
                                lastMoistureValue = moistureValue; // For testing can be 0 or 1 or back to moistureValue
                              }
                            }
                          
                            wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
                            {
                              //--- Rain sensor ---//
                              digitalWrite(RAIN_POWER_PIN, HIGH); // Turn rain power pin on
                              wait(200); // Set a delay to ensure the moisture sensor has powered up
                              int rainValue = digitalRead(DIGITAL_INPUT_RAIN); // Read digital rain value
                              digitalWrite(RAIN_POWER_PIN, LOW); // Turn rain power pin off
                              if (rainValue != lastRainValue) // Check value against saved value
                              {
                          #ifdef MY_DEBUG
                                Serial.print("Rain Sensor: ");
                                Serial.println(rainValue == 0 ? 1 : 0);
                          #endif
                                send(msg2.set(rainValue == 0 ? 1 : 0)); // Send the inverse
                                wait(RADIO_PAUSE);
                                lastRainValue = rainValue; // For testing can be 0 or 1 or back to rainValue
                              }
                            }
                          
                            wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
                            {
                              //--- Light sensor ---//
                              uint16_t lux = lightSensor.readLightLevel(); // Get Lux value
                              if (lux != lastlux)
                              {
                          #ifdef MY_DEBUG
                                Serial.print("LUX: ");
                                Serial.println(lux);
                          #endif
                                send(msg3.set(lux));
                                wait(RADIO_PAUSE);
                                lastlux = lux;
                              }
                            }
                          
                            wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
                            {
                              //--- Distance sensor ---//
                              int dist = metric ? sonar.ping_cm() : sonar.ping_in();
                              if (dist != lastDist)
                              {
                          #ifdef MY_DEBUG
                                Serial.print("Distance: ");
                                Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
                                Serial.println(metric ? " cm" : " in");
                          #endif
                                send(msg4.set(dist));
                                wait(RADIO_PAUSE);
                                lastDist = dist;
                              }
                            }
                          
                            //--- Analyse readings and set neopixel & booleans ---//
                            if (lastMoistureValue == 0 || lastRainValue == 0)
                            {
                              MowerWaiting = true;
                          #ifdef MY_DEBUG
                              Serial.print("Mower Waiting: ");
                              Serial.println(MowerWaiting);
                          #endif
                            }
                          
                            if ( timerActive == false && MowerWaiting == true ) // Logic for timer trigger
                            {
                              timerActive = true;
                              timerStarted = millis();
                              timer1Activated = false;
                              timer2Activated = false;
                              timer3Activated = false;
                              strip1.setPixelColor(0, 255, 0, 0);
                              strip1.setPixelColor(1, 255, 0, 0);
                              strip1.setPixelColor(2, 255, 0, 0);
                              strip1.setPixelColor(3, 255, 0, 0);
                              strip1.show();
                          #ifdef MY_DEBUG
                              Serial.print("Mower Is Waiting: ");
                              Serial.println("Activating Countdown");
                          #endif
                            }
                          
                            if ( timerActive == true )
                            {
                          #ifdef MY_DEBUG
                              Serial.print("Countdown: ");
                              Serial.println("Active");
                          #endif
                              unsigned long cm = millis();
                              if ( timerStarted + TIMER1 < cm && timer1Activated == false )
                              {
                                timer1Activated = true;
                                strip1.setPixelColor(0, 255, 0, 0);
                                strip1.setPixelColor(1, 255, 0, 0);
                                strip1.setPixelColor(2, 255, 0, 0);
                                strip1.setPixelColor(3, 0, 0, 0);
                                strip1.show();
                          #ifdef MY_DEBUG
                                Serial.print("3 Hours Left: ");
                                Serial.println("");
                          #endif
                              }
                              if ( timerStarted + TIMER2 < cm && timer2Activated == false )
                              {
                                timer2Activated = true;
                                strip1.setPixelColor(0, 255, 0, 0);
                                strip1.setPixelColor(1, 255, 0, 0);
                                strip1.setPixelColor(2, 0, 0, 0);
                                strip1.setPixelColor(3, 0, 0, 0);
                                strip1.show();
                          #ifdef MY_DEBUG
                                Serial.print("2 Hours Left: ");
                                Serial.println("");
                          #endif
                              }
                              if ( timerStarted + TIMER3 < cm && timer3Activated == false )
                              {
                                timer3Activated = true;
                                strip1.setPixelColor(0, 255, 0, 0);
                                strip1.setPixelColor(1, 0, 0, 0);
                                strip1.setPixelColor(2, 0, 0, 0);
                                strip1.setPixelColor(3, 0, 0, 0);
                                strip1.show();
                          #ifdef MY_DEBUG
                                Serial.print("1 Hours Left");
                                Serial.println("");
                          #endif
                              }
                              if ( cm > 14400000 )
                              {
                                timerActive = false;
                                strip1.setPixelColor(0, 0, 255, 0);
                                strip1.setPixelColor(1, 0, 255, 0);
                                strip1.setPixelColor(2, 0, 255, 0);
                                strip1.setPixelColor(3, 0, 255, 0);
                                strip1.show();
                          #ifdef MY_DEBUG
                                Serial.print("Mower Is No Longer Waiting: ");
                                Serial.println("Stopping Timer");
                          #endif
                              }
                            }
                          
                            if (lastDist < 60)
                            {
                              MowerHome = true;
                          #ifdef MY_DEBUG
                              Serial.print("Mower Home: ");
                              Serial.println(MowerHome);
                              strip1.setPixelColor(4, 0, 127, 0);
                              strip1.setPixelColor(5, 0, 127, 0);
                              strip1.setPixelColor(6, 0, 127, 0);
                              strip1.setPixelColor(7, 0, 127, 0);
                              strip1.show();
                          #endif
                            }
                          
                            else
                            {
                              MowerHome = false;
                          #ifdef MY_DEBUG
                              Serial.print("Mower Home: ");
                              Serial.println(MowerHome);
                              strip1.setPixelColor(4, 255, 0, 0);
                              strip1.setPixelColor(5, 255, 0, 0);
                              strip1.setPixelColor(6, 255, 0, 0);
                              strip1.setPixelColor(7, 255, 0, 0);
                              strip1.show();
                          #endif
                            }
                          
                            wait(LOOP_PAUSE); // Sleep or wait (repeater)
                          }
                          

                          Edit: I'm guessing the IF command should be "<" rather than ">" , fixed that. Check the code above for what I'm working on.

                          The issue I see right now is that if either moisture or rain is detected the mower timer will be reset each loop (that's okay), how can I also reset the countdown timer you created that I've implemented so they're in sync?

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

                          @Mark-Swift It's hard to help, sence the sketch is getting large. But From what I'm seeing, you do not store the lastvalues. You allways assing 0 to them

                             if (rainValue != lastRainValue) // Check value against saved value
                              {
                          #ifdef MY_DEBUG
                                Serial.print("Rain Sensor: ");
                                Serial.println(rainValue == 0 ? 1 : 0);
                          #endif
                                send(msg2.set(rainValue == 0 ? 1 : 0)); // Send the inverse
                                wait(RADIO_PAUSE);
                          //      lastRainValue = 0; // For testing can be 0 or 1 or back to rainValue 
                                  lastRainValue = rainValue; // remember the last value we've read
                              }
                            }
                          

                          That would be my first step. If you don't change it, then the sketch will only respond to the sensor's reporting 1.

                          Also, when the timer is being started you set all led's with a max of 255 for the RED led. If I'm not mistaken, you wanted them to be off. So set the RED value also to 0.

                          We'll get there step by step.

                          1 Reply Last reply
                          0
                          • Mark SwiftM Offline
                            Mark SwiftM Offline
                            Mark Swift
                            wrote on last edited by Mark Swift
                            #21

                            @TheoL With regards the LED's, I slightly changed the logic so they count down, rather than up, i.e. they extinguish as each hour passes until the 4th hour.

                            I don't see an issue with the current rain sensor / moisture sensor logic, I'm ensuring a reading is taken, but not seeing it unless they've changed? The number you see there now is only because I was testing, normally they're either

                            lastMoistureValue = moistureValue OR lastRainValue = rainValue

                            The main issue I see is that I need to countdown to be in sync with

                            if (lastMoistureValue == 0 || lastRainValue == 0)

                            so if (lastMoistureValue == 0 || lastRainValue == 0) is true on each loop, then the countdown LED's are reset also.

                            I'll no doubt change the loop wait to much longer once deployed.

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


                            10

                            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